How do I ensure random generates different samples each trial?

3 views (last 30 days)
I fit data to a normal distribution and then sample from that distribution:
%[...]
normal.x = fitdist(InterfractionalMotion.x,'Normal');
normal.y = fitdist(InterfractionalMotion.y,'Normal');
normal.z = fitdist(InterfractionalMotion.z,'Normal');
%[...]
%[excerpt of a function called from inside a loop processing data per patient]
RandomMotion.x = cmtomm*random(normal.x,1,NoFractions);
RandomMotion.y = cmtomm*random(normal.y,1,NoFractions);
RandomMotion.z = cmtomm*random(normal.z,1,NoFractions);
I repeated the 'random' command to sample random motion for each patient. Now I have been asked to conduct another trial using different random numbers, i.e. another random sampling from the same fit distributions.
I recall reading something about 'random number seeds' previously. Given P patients with S samples each, is there some inherent order so that the same set of random numbers S will be called for a different patient? Do I need to do something to ensure that different random samples are taken, such as specify some different seed? The random help documentation does not specify any optional command regarding seeding, so I wonder if it is fine to simply execute the script again.
The article " Random Numbers in MATLAB " emphasizes the need to manage the random number seed, but it is not clear if random sampling from a distribution follows the same mechanism as random number generation. Presumably it does, but I wish to verify.
  1 Comment
Daniel Bridges
Daniel Bridges on 20 Feb 2018
" Generate Random Numbers That Are Different " appears to answer this question, but I'm not sure if random sampling is included in their discussion of random number generators like rand.

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 20 Feb 2018
Executing rand (or random or randi) will return new random numbers, uncorrelated to previously retrieved numbers (unless you seed the random number generator to often).
N = 5 ;
r = zeros(10000,N) ;
for k=1:10,
r(:,k) = rand(10000,1) ;
end
c = corrcoef(r) % all very low, independence of N...

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!