R:set.seed()的异常行为 [英] R: bizarre behavior of set.seed()

查看:60
本文介绍了R:set.seed()的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在set.seed(0)和set.seed(1);中在R中发生奇怪的事情

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

将种子从0更改为1时,我得到的序列完全相同,但是移了1个单元格!

When changing the seed from 0 to 1, I get the exact same sequence, but shifted over by 1 cell!

请注意,如果我执行set.seed(2),我确实会得到似乎完全不同的(随机?)向量.

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?

推荐答案

这适用于Mersenne-Twister RNG的R实现.

This applies to the R implementation of the Mersenne-Twister RNG.

set.seed()接受提供的种子并将其加扰(在C函数 RNG_Init ):

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

然后将那个加扰的数字(seed)加扰625次,以填写Mersenne-Twister的初始状态:

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;
}

我们可以使用.Random.seed检查RNG的初始状态:

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)

从表中您可以看到有很多重叠.将此与seed = 3:

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)

回到0和1的情况,如果我们检查状态本身(忽略向量的前两个元素,这些元素不适用于我们正在查看的内容),则可以看到状态偏移了一个:

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

由于sample()选择的值基于这些数字,因此您会得到奇怪的行为.

Since the values selected by sample() are based on these numbers, you get the odd behavior.

这篇关于R:set.seed()的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆