Different Set.seed sample function results with same versions of R in different OS

Viewed 302

I have tried to use the sample function after setting seed, and run the code in a Mac computer and windows 10 computer respectively. However, there are different results when using the sample function even setting seed.

For Example:

In Mac Computer:

> set.seed(1)
> sample(100,3)
[1] 68 39 1

In Windows Computer:

> set.seed(1)
> sample(100,3)
[1] 27 37 57

However, the rnorm function has worked properly:

> set.seed(2)
> rnorm(3)
[1] -0.8969145  0.1848492  1.5878453

Is there anybody facing the same problem? R version is 4.1.1.

2 Answers

A likely cause of this problem is that one of the computers is still using the buggy sample() algorithm, because it was saved in a workspace and reloaded when you started R.

You can see which sample() algorithms are being used by running RNGkind(). I see [1] "Mersenne-Twister" "Inversion" "Rejection" , indicating that I'm using the Mersenne twister for uniforms, inversion for normals and rejection for discrete sampling.

You have to set also the kind:

set.seed(1, "Mersenne-Twister", sample.kind="Rounding")
sample(100,3)
#[1] 27 37 57

set.seed(1, "Mersenne-Twister", sample.kind="Rejection")
sample(100,3)
#[1] 68 39  1
Related