Despite set.seed - I get different results than others in R

Viewed 113

When doing

set.seed(123)
smaple(1:100,3)

in R, I get 29 79 41 which is not what others are getting. Why is this? And how can I change it?

When typing sessionInfo() I get a line which says

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding

Is that perhaps a problem?

1 Answers

We can change the default values in RNGkind

-default settings in R 4.1.0 - MacOS (which could vary based on R version - see below)

RNGkind()
#[1] "Mersenne-Twister" "Inversion"        "Rejection"    

-testing

set.seed(123)
sample(1:100, 3)
#[1] 31 79 51

-Change the values in RNGkind to match with the OP's

RNGkind(kind = "Mersenne-Twister",
        normal.kind = "Inversion", 
        sample.kind = "Rounding")

-test again

set.seed(123)
sample(1:100, 3)
[1] 29 79 41

According to ?RNGkind

sample.kind can be "Rounding" or "Rejection", or partial matches to these. The former was the default in versions prior to 3.6.0: it made sample noticeably non-uniform on large populations, and should only be used for reproduction of old results. See PR#17494 for a discussion.

Related