Odd thing happens when in R when I do set.seed(0) and set.seed(1);
set.seed(0)
sample(1:100,size=10,replace=TRUE)
#### [1] 90 27 38 58 91 21 90 95 67 63
set.seed(1)
sample(1:100,size=10,replace=TRUE)
#### [1] 27 38 58 91 21 90 95 67 63 7
When changing the seed from 0 to 1, I get the exact same sequence, but shifted over by 1 cell!
Note that if I do set.seed(2), I do get what appears to be a completely different (random?) vector.
set.seed(2)
sample(1:100,size=10,replace=TRUE)
#### [1] 19 71 58 17 95 95 13 84 47 55
Anyone know what's going on here?
This applies to the R implementation of the Mersenne-Twister RNG.
set.seed()
takes the provided seed and scrambles it (in the C function RNG_Init):That scrambled number (
seed
) is then scrambled 625 times to fill out the initial state for the Mersenne-Twister:We can examine the initial state for the RNG using .Random.seed:
You can see from the table that there is a lot of overlap. Compare this to
seed = 3
:Going back to the case of 0 and 1, if we examine the state itself (ignoring the first two elements of the vector which do not apply to what we are looking at), you can see that the state is offset by one:
Since the values selected by
sample()
are based on these numbers, you get the odd behavior.As you can see from the other answer, seeds
0
and1
result in almost similar initial states. In addition, Mersenne Twister PRNG has a severe limitation - "almost similar initial states will take a long time to diverge"It is therefore advisable to use alternatives like WELL PRNG (which can be found in randtoolbox package)