Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same seed, different OS, different random numbers in R

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 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) Is it an issue with R's versions or something more involved?
  • (2) How can this inconsistent behaviour be avoided? Any help is appreciated.

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
like image 895
epsilone Avatar asked Feb 05 '18 15:02

epsilone


1 Answers

From docs:

Random docs:

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
like image 50
twedl Avatar answered Oct 05 '22 23:10

twedl