vendredi 7 juillet 2017

How are random numbers generated in R when random seed is fixed?

I am running some stochastic simulation experiments and in one step I want to estimate the correlation between random numbers when the underlying source of randomness is the same, i.e., common U(0,1) random numbers.

I thought the following two code segments should produce the same result.

set.seed(1)
a_1 = rgamma(100, 3, 4)
set.seed(1)
b_1 = rgamma(100, 4, 5)
cor(a_1,b_1)

set.seed(1)
u = runif(100)
a_2 = qgamma(u, 3, 4)
b_2 = qgamma(u, 4, 5)
cor(a_2,b_2)

But the results are different

> cor(a_1,b_1)
[1] 0.9908946
> cor(a_2,b_2)
[1] 0.9995341

Can anyone explain why the results are different? I suspect this has something to do with how random variables are generated in R with the "rxxx" functions, but I couldn't find the details...

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire