exponential random draws and the seed
Show older comments
Hello,
I have a question about setting a seed on random numbers. I have tried
s = RandStream('mt19937ar','Seed','shuffle');
RandStream.setGlobalStream(s);
but it does not work obviously.
Is there embedded seed that I can use in drawing exponential random variables like rand 'twitter' 'seed' type of stuff?
Thanks
Answers (3)
Walter Roberson
on 16 Feb 2012
0 votes
The 'shuffle' keyword was not added until R2011a
I do not understand about the reference to twitter.
You can use any integer seed up to 2^32-1
Honglei Chen
on 16 Feb 2012
What's your version of MATLAB? setGlobalStream is introduced in R2011a, try
RandStream.setDefaultStream(s)
3 Comments
etcann
on 16 Feb 2012
Walter Roberson
on 16 Feb 2012
The stream advances as you use it. Setting it as a default stream does not in any way reset it. Default stream is just a matter of which stream is used by default if the code does not specifically ask for a different stream to be used.
If you call your "default pizza delivery store", you always get a current pizza made fresh; re-setting your default pizza delivery store back to the same store does not cause the store to go back and start re-making pizzas exactly like the old ones again.
Compare:
s = RandStream('mt19937ar','Seed',100);
RandStream.setDefaultStream(s);
U1=exprnd(1, [n,g] );
s = RandStream('mt19937ar','Seed',100);
RandStream.setDefaultStream(s);
U2=exprnd(1, [n,g] );
Now U1 and U2 should be the same: you reset back to the same conditions.
Honglei Chen
on 17 Feb 2012
@Walter, you are right, and thanks for pointing that out. I was not reading the comment correctly. I'm removing my comment.
Peter Perkins
on 17 Feb 2012
etcann, It appears you are using MATLAB 2010a. That release pre-dates the introduction of RandStream.setGlobalStream, which is except for the name is identical to the older RandStream.setDefaultStream. The new name was added because people found the "default" in setDefaultStream to be confusing.
If you were using R2011a, I'd suggest that you use the RNG function, which is a bit more like the even older (and now discouraged) rand('twister',seed).
Walter's suggestion would work, or even simpler,
s = RandStream('mt19937ar','Seed',100);
RandStream.setDefaultStream(s);
U1=exprnd(1, [n,g] );
reset(s);
U2=exprnd(1, [n,g] );
The point is that the random stream that you create in the first line, and make the global default in the second, changes it's state when you call exprnd. So you have to reset it, or create a new one, if you want to go back to where you started.
8 Comments
etcann
on 17 Feb 2012
Walter Roberson
on 17 Feb 2012
etcann, if you are a student, wait another few months and then buy the R2012a Student Version. If you are not a student, then usually the least expensive way to upgrade is to pay software maintenance between your version and the current version.
Walter Roberson
on 19 Feb 2012
Peter, reset(s) would reset to the default initial state, not to the state based upon the seed of 100. In order to change to reusing the 100 seed, the code would need to be changed from reset(s) to reset(s,100)
Peter Perkins
on 19 Feb 2012
etcann, there's nothing that RNG can do that RandStream can't. So technically, you don't really need it. However, RNG is much simpler (which is why we added it).
Walter, that's not quite right: reset resets to whatever seed the stream was created with. Most of the time, that would be the "factory default" start-up state, but in my above code, the output from the two lines that call exprnd really is identical.
Walter Roberson
on 20 Feb 2012
> reset resets to whatever seed the stream was created with
"reset(s) resets the generator for the random stream s to its initial internal state."
That line is, in my opinion, not at all clear about whether the "initial internal state" includes the seed. It doesn't rule it out, but I do not see any documentation about what is in the "initial internal state", and I have encountered random stream resets that did not go back to the user-supplied seed so it isn't obvious.
This wording also suggests that if you create a stream and then in the _next_ call set() the seed for it, that the "initial internal state" would be the one using the default see rather than the last seed that was set. One would thus be advised to reset(s,seed) rather than to count on the seed having been part of the initial state 'atomically'.
Peter Perkins
on 21 Feb 2012
Walter, you're right on a couple of points:
* The reference page probably ought to say something like, "... to the internal state corresponding to its seed."
* I should have said, "reset with no arguments resets the stream to the internal state corresponding to the seed with which the stream was created, or to the seed that you most recently reset the stream with using the two-arg form of reset." I was being imprecise.
But I'm not following your last paragraph. The seed property of a stream is not directly writable, the only way you can set or reset it is when you create a stream, or if you go out of your way to use reset with a second argument. The documentation tries to discourage the latter, partly because you definitely don't want to do it if your stream is part of a collection of parallel streams, and partly because we found that people reseed way too much. So:
>> s = RandStream('mrg32k3a','Seed',0)
s =
mrg32k3a random stream
Seed: 0
NormalTransform: Ziggurat
>> rand(s,1,4)
ans =
0.72701 0.45218 0.93871 0.23604
>> reset(s)
>> rand(s,1,4)
ans =
0.72701 0.45218 0.93871 0.23604
>> reset(s,12345)
>> s
s =
mrg32k3a random stream
Seed: 12345
NormalTransform: Ziggurat
>> rand(s,1,4)
ans =
0.13054 0.28386 0.49401 0.60627
>> reset(s)
>> rand(s,1,4)
ans =
0.13054 0.28386 0.49401 0.60627
Are you seeing something other than that?
Walter Roberson
on 21 Feb 2012
My concern was about the missing "or to the seed that you most recently reset the stream with <etc>" (as such a seed would, by definition, not be part of the "initial internal state"). The behavior you describe now does make sense.
Peter Perkins
on 21 Feb 2012
I've made a note to have that reference page clarified. Thanks for pointing it out.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!