Fix the seed in Matlab R2011b | Why RandStream failed?
2 views (last 30 days)
Show older comments
I employed the following codes to fix the seed in Matlab R2011b
s = RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(s);
Then I tried to draw some uniform/normal/wishart random numbers in thousands of iterations, but the random numbers are the same only for first iteration when I restart Matlab. The iterations are like:
for i = 1: n
a = rand(1,1);
b = randn(3,1);
c = wishrnd(omega,t);
end
Though it doesn't work within my complicated codes of real interest, I experimented with a simpler version as the above and it works whenever I restart Matlab.
I am really confused what is the source of this failure. Any insights are appreciated. Many thanks.
Allen
0 Comments
Answers (4)
the cyclist
on 1 Aug 2012
Edited: the cyclist
on 1 Aug 2012
The rng() function is available in R2011b, and is the recommended method for seeding the random number generators.
doc rng
for details.
Peter Perkins
on 1 Aug 2012
Allen, I can't tell what you mean by, "the random numbers are the same only for first iteration when I restart Matlab." You may mean that you expect to be able to restart MATLAB, run the same code, and see exactly the same numbers in all iterations of the loop, and what you actually see is that they are the same only for the first iteration. If you are really executing those first two lines that set the global random number stream, then you should get repeatable results. In fact, since those two lines do exactly what MATLAB already does at startup, you should see repeatable results even if you don't execute them.
It would be good if you would provide more specific information.
If the above is what you mean, then there must be something going on in your code that you have not mentioned. Especially since you can't reproduce the prblem in a simple example. My suggestion would be to set a breakpoint somewhere in your code at the point when you think things have gone wrong. When execution stops at that breakpoint, check and see what the global stream is, because it's possible that code that you are calling is changing it.
Beyond that, you're going to have to be more specific. Hope this helps.
Allen Liu
on 2 Aug 2012
Edited: Allen Liu
on 2 Aug 2012
1 Comment
Honglei Chen
on 2 Aug 2012
Hi Allen, as I explained in the other forum where you posted the same question, it is unlikely that people here will have time to read your script especially it doesn't wrong without your Excel file. You need to provide simple reproduction steps. Peter has given excellent advice above regarding how to debug your script. I suspect that certain function in your script calls some other functions that alter the random number generator.
Honglei Chen
on 2 Aug 2012
Do you have to set it to global stream? If not, why not generate those random numbers in form of
rand(s,1,n)
so you by pass the possibility that other things mess up the random number state behind the scene?
3 Comments
Honglei Chen
on 2 Aug 2012
Sorry for the confusion, s stands for stream. Something like this
rs = RandStream.create('mt19937ar');
rand(rs,1,10)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!