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(1000)
a_1 = rgamma(100, 3, 4)
set.seed(1000)
b_1 = rgamma(100, 4, 5)
cor(a_1,b_1)
set.seed(1000)
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.04139218
> cor(a_2,b_2)
[1] 0.9993478
Which a fixed random seed, I expect the correlations to be close to 1 (as it is the case in the second code segment). However, the outputs in the first code segment is surprising
For this specific seed (1000), the correlation in the first segment has a negative sign and a very small magnitude. Neither the sign nor the magnitude makes sense...
When playing around with different seeds (e.g., 1, 10, 100, 1000), the correlation in the first segment changes significantly while the correlation in the second segment is quite stable.
Can anyone give some insights about how R samples random numbers from the same seed?
Thanks in advance!