MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

outputs from randg not repeatable with the same random seed

1 view (last 30 days)
Etsuko
Etsuko on 25 Apr 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, I have a lengthy simulation program composed of many m-files (in total about 1000 lines). It uses several different random number generators (rand, randn, poissrnd, gamrnd, etc) and use these very numerous times. I set a random seed using rng at the very beginning of the main program that calls other m-files. I just noticed that outcomes from the program were not repeatable even though I use the same seed. I examined the code carefully and pinpointed that the matlab function randg is causing this problem. I placed rand(1) just before and after randg and sometimes (not always) rand(1) placed after randg gives a different number. Strangly, the different numbers are not any number; it is from a small set of numbers. Although I ran my code many times to check this issue, so far I have seen only 3-4 different numbers generated from the rand(1) placed after randg. Has anyone encountered this type of problems with randg before? What would be the reason for this? Would it be possible there may be some numerical errors in randg function? Is there anyway this problem can be resolved? Thank you.

Answers (1)

Steven Lord
Steven Lord on 25 Apr 2016
I can't reproduce this behavior. Please post the following information:
  • Which release of MATLAB and Statistics and Machine Learning Toolbox (previously just called Statistics Toolbox) are you using?
  • What operating system are you using? Be as specific as you can; for instance, if you're using Windows say whether you're using Windows 7, Windows 8, Windows 10, etc.
  • What is the output of this command:
which -all randg
  • Show a SMALL section of code (no more than 10 lines) that demonstrates the problem.
  1 Comment
Etsuko
Etsuko on 25 Apr 2016
Hi Steven,
I am using Matlab R2015a student version with the stat toolbox that came with it. The operating system is OS X Yosemite 10.10.5.
The output of the command:
/Applications/MATLAB_R2015a.app/toolbox/stats/stats/randg.mexmaci64 /Applications/MATLAB_R2015a.app/toolbox/stats/stats/randg.m % Shadowed
Small code to demonstrate: I tried it but so far I cannot reproduce it. I can reproduce it with the simulation program. I used this one and has been unsuccessful.
seedi = 984;
s = RandStream('mt19937ar','Seed',seedi);
RandStream.setGlobalStream(s);
for i = 1:2:1000000
test(i)=rand(1);
r = randg(400*WIJ',sizeOut);
test(i+1)=rand(1);
end
s = RandStream('mt19937ar','Seed',seedi);
RandStream.setGlobalStream(s);
for i = 1:2:1000000
test2(i)=rand(1);
r = randg(400*WIJ',sizeOut);
test2(i+1)=rand(1);
end
sum(test-test2)
One clarification: My description was a bit ambiguous. I stated "Although I ran my code many times to check this issue, so far I have seen only 3-4 different numbers generated from the rand(1) placed after randg." This is referring to randg and rand(1) combo placed EXACTLY at the same place in the iterating program (i.e., same time point). I do not mean that rand(1) placed right after randg produces only 3-4 different numbers.
Thank you.
Etsuko

Community Treasure Hunt

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

Start Hunting!