相同的种子,不同的操作系统,R中的不同随机数

时间:2018-02-05 15:36:54

标签: r random operating-system random-seed

我在两台机器和一台linux服务器之间遇到了不一致的结果,直到我意识到修复种子有不同的效果。我在所有版本中都运行了不同的R版本,都在3.3.0以上。以下是示例:

Linux 1

> set.seed(10); rnorm(1)
[1] -0.4463588
> version
               _
platform       x86_64-pc-linux-gnu
arch           x86_64
os             linux-gnu
system         x86_64, linux-gnu
status
major          3
minor          3.0
year           2016
month          05
day            03
svn rev        70573
language       R
version.string R version 3.3.0 (2016-05-03)
nickname       Supposedly Educational

Linux 2

> set.seed(10); rnorm(1)
[1] 0.01874617
> version
               _
platform       x86_64-pc-linux-gnu
arch           x86_64
os             linux-gnu
system         x86_64, linux-gnu
status
major          3
minor          4.2
year           2017
month          09
day            28
svn rev        73368
language       R
version.string R version 3.4.2 (2017-09-28)
nickname       Short Summer

Mac OS

> set.seed(10); rnorm(1)
[1] 0.01874617
> version
               _                           
platform       x86_64-apple-darwin15.6.0   
arch           x86_64                      
os             darwin15.6.0                
system         x86_64, darwin15.6.0        
status                                     
major          3                           
minor          4.3                         
year           2017                        
month          11                          
day            30                          
svn rev        73796                       
language       R                           
version.string R version 3.4.3 (2017-11-30)
nickname       Kite-Eating Tree        

> set.seed(10); rnorm(1)
[1] 0.01874617
> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          4.1                         
year           2017                        
month          06                          
day            30                          
svn rev        72865                       
language       R                           
version.string R version 3.4.1 (2017-06-30)
nickname       Single Candle               

Linux从同一种子生成不同的随机数,从而使得在其上运行的脚本的结果不能完全重现(取决于重新运行它们的操作系统,结果是否一致)。这很烦人。

我不知道这里发生了什么。特别是:

  • (1)这是R版本的问题还是更复杂的问题?
  • (2)如何避免这种不一致的行为?任何帮助表示赞赏。

编辑源自@Jesse Tweedle答案(Linux 1在新会话中的输出):

> set.seed(10); rnorm(1)
[1] -0.4463588
> set.seed(10); rnorm(1)
[1] -0.4463588
> set.seed(102); rnorm(1)
[1] 0.05752965
> set.seed(10, kind = "Mersenne-Twister"); rnorm(1)
[1] 0.01874617
> set.seed(10); rnorm(1)
[1] 0.01874617
> set.seed(102); rnorm(1)
[1] 0.1805229

1 个答案:

答案 0 :(得分:9)

来自docs:

Random docs

RNGversion can be used to set the random generators as they were in an earlier R version (for reproducibility).

所以在所有系统上试试这个:

set.seed(10, kind = "Mersenne-Twister", normal.kind = "Inversion"); rnorm(1)
[1] 0.01874617