简体   繁体   English

R.2.15.2的set.seed

[英]set.seed with R 2.15.2

My understanding is that using set.seed ensures reproducibility but this is not the case with the following R code in R 2.15.2 . 我的理解是,使用set.seed可确保可重复性,但对于R 2.15.2的以下R代码,情况并非如此。 Am I missing something here? 我在这里想念什么吗?

set.seed(12345)
rnorm(5)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875
 rnorm(5)
[1] -1.8179560  0.6300986 -0.2761841 -0.2841597 -0.9193220

set.seed() reinitializes the random number generator . set.seed()重新初始化随机数生成器

set.seed(12345)
rnorm(5)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875

set.seed(12345)
rnorm(5)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875

set.seed(12345)
rnorm(5)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875

Any call that uses the random number generator will change the current seed, even if you've manually set it with set.seed . 即使使用set.seed手动设置了种子,任何使用随机数生成器的调用都将更改当前种子。

set.seed(1)
x <- .Random.seed # get the current seed
runif(10) # uses random number generator, so changes current seed
y <- .Random.seed
identical(x, y) # FALSE

As @StephanKolassa demonstrates, you'd have to reset the seed before each use of the random number generator to guarantee that it uses the same one each time. 正如@StephanKolassa演示的那样,您必须在每次使用随机数生成器之前重置种子,以确保每次都使用相同的种子。

It's worth underlining here that the sequence of numbers is still reproducible each time you set the seed, because of this reinitialisation. 在此值得强调的是,由于这种重新初始化,每次设置种子时数字序列仍可重现。

So although with each subsequent call to eg rnorm you're getting different answers to each call, you're still going to get the same sequence of numbers from the point the seed was set . 因此,尽管以后每次调用rnorm您对每个调用都会得到不同的答案, 但是从设置种子的那一刻起您仍将获得相同的数字序列

Eg, per the original question: 例如,根据原始问题:

set.seed(12345)
rnorm(5)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875
rnorm(5)
[1] -1.8179560  0.6300986 -0.2761841 -0.2841597 -0.9193220

Produces the same sequence of 10 numbers as: 产生与10相同的数字序列,如下所示:

set.seed(12345)
rnorm(10)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875
-1.8179560  0.6300986 -0.2761841 -0.2841597 -0.9193220

Or 要么

set.seed(12345)
rnorm(7)
[1]  0.5855288  0.7094660 -0.1093033 -0.4534972  0.6058875
-1.8179560  0.6300986
rnorm(3)
[1] -0.2761841 -0.2841597 -0.9193220

Or whatever series of calls to rnorm . 或对rnorm任何系列调用。

The point here is that if you set the seed once at the start of a script you will get the same set of random numbers generated each time you run that whole script , while getting a different set of numbers from each random number generator call within the code. 此处的要点是, 如果您在脚本开始时设置一次种子,则每次运行整个脚本都会获得相同的随机数集 ,而在码。 This is because you are running on the same sequence from that seed at the start. 这是因为从一开始,您就从该种子开始以相同的顺序运行。 This can be a good thing, and it means that if you want a reproducible script you can set the seed once at the beginning. 这可能是一件好事,这意味着如果您想要可复制的脚本,则可以在开始时设置一次种子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM