R: bizarre behavior of set.seed()

﹥>﹥吖頭↗ 提交于 2019-11-30 18:57:47

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):

for(j = 0; j < 50; j++)
  seed = (69069 * seed + 1);

That scrambled number (seed) is then scrambled 625 times to fill out the initial state for the Mersenne-Twister:

for(j = 0; j < RNG_Table[kind].n_seed; j++) {
  seed = (69069 * seed + 1);
  RNG_Table[kind].i_seed[j] = seed;
}

We can examine the initial state for the RNG using .Random.seed:

set.seed(0)
x <- .Random.seed

set.seed(1)
y <- .Random.seed

table(x %in% y)

You can see from the table that there is a lot of overlap. Compare this to seed = 3:

set.seed(3)
z <- .Random.seed

table(z %in% x)
table(z %in% y)

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:

x[3:10]
# 1280795612 -169270483 -442010614 -603558397 -222347416 1489374793  865871222
# 1734802815

y[3:10] 
# -169270483 -442010614 -603558397 -222347416 1489374793  865871222 1734802815
# 98005428

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 and 1 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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!