# How to duplicate rand('twister', 0) in current Matlab?

15 views (last 30 days)
Kevin on 6 Feb 2012
Hi everyone,
I am trying to change my old code to use the new RNG in current Matlab (R2011a).
In my old code, I usually have something like this,
>> rand('twister', 1);
>> x = rand(1, 1e5);
So I have tried the following experiment to verify that RNG will give me the same random numbers,
>> rng(1, 'twister'); x1 = rand(1,100);
>> rand('twister', 1); x2 = rand(1,100);
>> max(abs(x1 - x2))
ans =
0
So this works for seed = 1. However when I try setting seed to 0, then they do not match,
>> rng(0, 'twister'); x1 = rand(1,100);
>> rand('twister', 0); x2 = rand(1,100);
>> max(abs(x1 - x2))
ans =
0.817037959716122
A while back (probably year ago), I have posted some comment about random stream (back then, this is a new feature) and seed of 0 have come up in the discussion. But I cannot find the discussion any more.
So my question: how can I use RNG to duplicate the effect of rand('twister', 0)?
Also besides 0, is there any other seed having this problem?
Thanks Kevin

Peter Perkins on 6 Feb 2012
Historically, using 0 as a seed for MATLAB's random number generators meant, under the hood, "use the special seed that MATLAB uses at startup". It did not mean to literally use the value zero as a seed, because for one thing, 0 was not a valid seed for most of those generators. 'twister', starting in R14 or so, was the one exception to that, 0 meant 0. You probably used 0 in your code because all the older generators had that convenience built in. But to my knowledge, the documentation actually never showed that -- always 5489 or something else non-zero.
Beginning in R2008a (or thereabouts), the Mersenne Twister that you get via the RandStream class, and therefore via the RNG function, goes back to the old meaning for 0: "use the seed that MATLAB uses at startup", which happens to be 5489, the seed that for whatever reasons, the MT designers used as their default.
So the short answer is that you can't directly use RNG to duplicate the effect of rand('twister', 0). 0 for the MT is the only case of this. The longer answer is that you could use the old code, read the state using the old syntax, and muck around with the structure that you get back from RNG. But I recommend not getting into those low-level details.

Kevin on 6 Feb 2012
Hi Peter,
Thank you very much for your explanation. It helps a lot. I believe it was you answering my post in the newsgroup a while back (when Matlab introduced RandStream to the world).
So would the following table works if I want to upgrade my old code to use RNG,
Old seed used in New seed used in
rand('twister', seed) rng(seed, 'twister')
----------------------- ----------------------
0 No answer. Avoid this case
1 1
2 2
.... ....
5488 5488
5489 0
5490 5490
.... ....
(2^32) - 1 (2^32) - 1
Here I assume that the twister seed is a unsiged 32-bit integer. Please let me know if I am wrong on this.
Hope that Mathworks would not obsolete the old usage of rand('twister', seed) and randn in the future.
Thanks a lot !!!!
Kevin
Kevin on 6 Feb 2012
Oh the formatting mess up my table. I will re-post the table as an "answer".

Sean de Wolski on 6 Feb 2012
0 is the same as the MT default in the original paper, 5489.
rng(0,'twister'); x1 = rand(1,100);
rand('twister', 5489); x2 = rand(1,100);
isequal(x1,x2)

#### 1 Comment

Kevin on 6 Feb 2012
Hi Sean,
I need to map the "old" seed to the "new" seed.
For example, I know that if my old code uses,
rand('twister', 1);
then in the new code, I can use the following,
rng(1, 'twister');
However, what should I use if in the old code I have the following,
rand('twister', 0);

Kevin on 6 Feb 2012
This is related to my comment to Peter's answer.
So would the following table works if I want to upgrade my old code to use RNG,
Old seed used in New seed used in
rand('twister', seed) rng(seed, 'twister')
----------------------- ----------------------
0 No solution. Avoid this case.
1 1
2 2
..... .....
5488 5488
5489 0
5490 5490
...... ......
(2^32) - 1 (2^32) - 1
Here I assume that the twister seed is a unsiged 32-bit integer. Please let me know if I am wrong on this.

Peter Perkins on 7 Feb 2012
Yes, although
* The seed need not be an unsigned 32 bit integer type, it can be any numeric type. It just has to be an integer _value_. So type "1", not "uint32(1)".
* 5489 is perfectly acceptable as itself. 0 is just a shorthand.
Kevin on 7 Feb 2012
Yes, you are right. I should be more careful.
So seed is a double whose valid values are: 0, 1, 2, ..., ((2^32) - 1). But we should avoid 0 in the old usage of rand.
Thank you very much for your help.