R中相同的种子,不同的操作系统,不同的随机数 [英] Same seed, different OS, different random numbers in R

查看:539
本文介绍了R中相同的种子,不同的操作系统,不同的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was experiencing inconsistent results between two machines and a linux server, until I realized that fixing the seed was having different effects. I am running different R versions in all of them, all above 3.3.0. Here are the examples:

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        

Windows

> 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从同一种子提供了不同的随机数生成,因此使在其上运行的脚本的结果无法完全再现(取决于重新运行它们的OS,结果是否一致).这很烦人.

Linux gives a different random number generation from the same seed, thus making the result of a script run on it not fully reproducible (depending on the OS in which they are re-run, the results will agree or not). This is annoying.

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

I do not know what is happening here. Particularly:

  • (1)R的版本是否有问题?
  • (2)如何避免这种不一致的行为?感谢您的帮助.
  • (1) Is it an issue with R's versions or something more involved?
  • (2) How can this inconsistent behaviour be avoided? Any help is appreciated.

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

EDIT originated from @Jesse Tweedle answer (output in Linux 1 in a new session):

> 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

推荐答案

来自文档:

随机文档:

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

因此,请在所有系统上尝试此操作:

So try this on all systems:

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

这篇关于R中相同的种子,不同的操作系统,不同的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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