[英]Is set.seed consistent over different versions of R (and Ubuntu)?
I am currently running R version 3.1.0 (on Ubuntu 12.04 LTS) and as both my R version and my operating system is getting rather old, I plan on updating both.我目前正在运行 R 版本 3.1.0(在 Ubuntu 12.04 LTS 上),由于我的 R 版本和我的操作系统都变得很旧,我计划更新两者。 However, I have a lot of simulations that rely on set.seed() and I would like them to still give me the same random numbers after updating both R and my operating system.但是,我有很多依赖于 set.seed() 的模拟,我希望它们在更新 R 和我的操作系统后仍然给我相同的随机数。
So my question is three-fold.所以我的问题是三重的。
If you installed R on two different operating systems without manually changing defaults or the RProfile
, you should get the same results when using set.seed()
.如果您在两个不同的操作系统上安装了 R 而没有手动更改默认值或RProfile
,则在使用set.seed()
时应该得到相同的结果。
It used to be the case that set.seed()
would give the same results across R versions, but that's no longer generally true thanks to a little-announced update in R 3.6.0.过去的情况是set.seed()
会在 R 版本中给出相同的结果,但由于 R 3.6.0 中的一个小宣布更新,这种情况不再普遍。 So you can get cross version consistency comparing results before R 3.6.0, but if you compare a post-3.6.0 use of set.seed()
to a pre-3.6.0 use of set.seed()
, you will get different results.因此,您可以获得 R 3.6.0 之前的跨版本一致性比较结果,但是如果将 3.6.0 之后的set.seed()
使用与 3.6.0 之前的set.seed()
使用进行set.seed()
,您将得到不同的结果。
You can see that in the examples below:您可以在下面的示例中看到:
> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"
> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"
set.seed(1999)
sample(LETTERS, 3)
[1] "D" "Z" "R"
The reason for the inconsistency is that in R 3.6.0, the default kind of under-the-hood random-number generator was changed .不一致的原因是在 R 3.6.0 中, 默认的底层随机数生成器类型已更改。 Now, in order to get the results from set.seed()
to match, you have to first call the function RNGkind(sample.kind = "Rounding")
.现在,为了从set.seed()
获得匹配的结果,您必须首先调用函数RNGkind(sample.kind = "Rounding")
。
> RNGkind(sample.kind = "Rounding")
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
> set.seed(1999)
> sample(Letters, 3)
[1] "T" "N" "L"
Having tested on several R versions (3.1.0, 3.3.1, 3.4.2) and two different machines (Windows 7 x64, Windows 10 x64), I got the same runif()
random numbers with the same set.seed()
independently of the R versions and the operating system.在多个 R 版本(3.1.0、3.3.1、3.4.2)和两台不同的机器(Windows 7 x64、Windows 10 x64)上进行测试后,我得到了相同的runif()
随机数和相同的set.seed()
独立于 R 版本和操作系统。 As far as I know, this suggests a yes for both questions 1 and 2.据我所知,这表明对问题 1 和 2 都是肯定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.