Different random number generation between OS

2019-06-17 05:57发布

问题:

Does anyone have any experience with situations where set.seed gives different results depending on operating system (OS). I remember coming across a similar situation in a class on R before where some people were generating different random sequences using rnorm despite setting the starting seed to the same value. Now, I'm giving a course myself and have not run into the same issue with rnorm; all my students get the same sequence regardless of OS. Interestingly, the same issue seems to exist with the mvrnorm function of the MASS package.

Any insight would be greatly appreciated - Marc

This example:

require(MASS)
set.seed(123)
a <- rnorm(10, mean=10, sd=3)
b <- rnorm(10, mean=5, sd=2)
df <- data.frame(a,b)
C <- cov(df)
M <- mvrnorm(n=10, c(10,5), C)

df
C
M

Yields on my Windows 7 OS 64-bit version of R 2.14.1.:

> df
           a        b
1   8.318573 7.448164
2   9.309468 5.719628
3  14.676125 5.801543
4  10.211525 5.221365
5  10.387863 3.888318
6  15.145195 8.573826
7  11.382749 5.995701
8   6.204816 1.066766
9   7.939441 6.402712
10  8.663014 4.054417
> C
         a        b
a 8.187336 3.431373
b 3.431373 4.310385
> M
              a        b
 [1,] 13.270535 6.158603
 [2,] 10.375011 5.737871
 [3,] 13.514105 5.476411
 [4,] 12.681956 5.020646
 [5,] 12.352333 4.927746
 [6,] 15.177508 6.810387
 [7,]  8.114377 2.925225
 [8,]  9.529744 4.834451
 [9,] 12.903550 7.232715
[10,]  6.251907 3.481789

Edit: It might be helpful to know if anyone is not getting these results and what OS or versions of R were used.

回答1:

I have heard of people changing the RNGKind, sometimes without realizing it by loading and running a package that changed the generator or some other script that made the change. If that was the case then the same seed would lead to different random numbers. A fresh running of R (without loading different packages or other scripts) should generate the same random numbers from the same seed.



标签: r random