17

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
989
  • 12,579
  • 5
  • 31
  • 53
epsilone
  • 745
  • 11
  • 23
  • 1
    See https://stackoverflow.com/questions/47199415/is-set-seed-consistent-over-different-versions-of-r-and-ubuntu for someone who finds consistent random numbers using set.seed across OSes / Rs. – twedl Feb 05 '18 at 15:43
  • So you know how R was installed on linux 1? Built from source? Did it use use custom flags during compilation? If using a standard build I would expect the random numbers to be the same across all (recent) builds. I don't think this has changed in R in a long time. – MrFlick Feb 05 '18 at 15:56
  • @MrFlick Unfortunately I do not know how `R` was installed on Linux 1. – epsilone Feb 05 '18 at 16:11

1 Answers1

12

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
twedl
  • 1,588
  • 1
  • 17
  • 28
  • So if you call `RNGkind()` on "Linux 1" it says `[1] "Mersenne-Twister" "Inversion"`? – twedl Feb 05 '18 at 16:02
  • This! I still do not know what was happening exactly, but this does the trick. (I edit the question for proper format). To your comment: yes it does. – epsilone Feb 05 '18 at 16:03
  • Ok, great. I forgot to add `normal.kind` argument at first (since you're generating random normals, and the second argument is specifically for normals). – twedl Feb 05 '18 at 16:05
  • 1
    @epsilone Please see the answer here for an explanation of why the two Linux results differ. https://stackoverflow.com/a/56381613/5373794 – bschneidr Sep 30 '21 at 17:56