1

I have seen a couple posts on the use of the set.seed() function followed by the sample() function, but it is still not clear to me why sample() gives different results.

I have replicated a very simple example using the code below and get different results based on the respective R versions.

How do I get the same sample() result consistently as this is important when sharing the code to be reproducible on another machine / platform.

set.seed(47)
index3 = sample(rnorm(200), 200, replace = FALSE)
index3

Version of 1st run (Windows):

##                _                           
## platform       x86_64-w64-mingw32          
## arch           x86_64                      
## os             mingw32                     
## system         x86_64, mingw32             
## status                                     
## major          3                           
## minor          5.3                         
## year           2019                        
## month          03                          
## day            11                          
## svn rev        76217                       
## language       R                           
## version.string R version 3.5.3 (2019-03-11)
## nickname       Great Truth

Version of 2nd run (Mac):

##                _                           
## platform       x86_64-apple-darwin17.0     
## arch           x86_64                      
## os             darwin17.0                  
## system         x86_64, darwin17.0          
## status                                     
## major          4                           
## minor          0.0                         
## year           2020                        
## month          04                          
## day            24                          
## svn rev        78286                       
## language       R                           
## version.string R version 4.0.0 (2020-04-24)
## nickname       Arbor Day
Aveshen Pillay
  • 431
  • 3
  • 13
  • 4
    Does this answer your question? [Is set.seed consistent over different versions of R (and Ubuntu)?](https://stackoverflow.com/questions/47199415/is-set-seed-consistent-over-different-versions-of-r-and-ubuntu) Despite the title, this issue (and the accepted answer to the proposed duplicate) has to do with R >= 3.6.0 vs R < 3.6.0. They changed the default `sample()` behavior in R 3.6.0. You're comparing runs from R 3.5.3 and R 4.0.0, so that's why – duckmayr May 28 '20 at 14:50
  • 2
    Note that the answer also shows how to make the results repeateable - in the newer version of R run `RNGkind(sample.kind = "Rounding")` to use the older `sample` behavior. – Gregor Thomas May 28 '20 at 14:58

0 Answers0