I am working on a Monte Carlo project, and in this matter, I have to draw a lot of random numbers. I have made two implementations, one where I draw the random numbers all at once and one here I draw them as part of a loop. My question appeared when I compared the outcome and became aware that they weren't the same. I immediately suspected the drawing of the random numbers to be the cause of the differences, and it appears to be the case from the study below.
x_2 <- matrix(0,n,t)
set.seed(123)
for(s in 1:t) {
x_2[, s] <-rnorm(5000,0,3)
if (s<=10){ print(rnorm(5000,0,3)[1:10])}
}
set.seed(123)
e_2 <- matrix(rnorm(5000 * 5000, 0, 3), 5000, 5000)
print(e_2[1:10,1:10])
The outcome of the above can be seen below.
[1] -1.482522 3.382780 -3.440849 4.443056 2.748574 1.005393 1.724026 0.610859 -1.341124 -1.030578
[1] -4.061547 -1.738132 -2.583133 2.918035 1.857437 4.156337 -4.461811 1.917578 1.124205 1.110564
[1] -2.3856225 -3.4053345 1.7403037 1.5541277 0.6230563 -1.4502206 -1.1568202 -1.3942097 3.6861666 -0.2023702
[1] -2.6641585 1.0807931 1.2662698 -2.2296928 0.5406639 2.8972701 1.0827109 -1.4748768 0.6064607 -0.7711647
[1] 2.9164836 -0.6704332 0.4415908 -0.6774140 1.7499817 2.9110134 0.1417097 -1.2382027 -1.2465868 1.5946641
[1] 1.24145410 4.74917343 -0.04523692 3.16943742 2.37547831 1.40694830 0.24448225 -1.65932819 1.13039946 -0.55707338
[1] 1.3946592 -1.0632645 -2.7107054 5.1341154 4.5597899 -2.9279816 -2.5342361 -2.8255961 -0.8627209 0.2887078
[1] 0.27116861 1.31304754 0.65551315 -0.04878318 -2.75273200 1.59058082 4.08446872 0.58036680 0.41595295 -4.32112988
[1] 4.342664 2.175384 7.098014 -4.497943 -3.546591 -1.108493 1.748281 2.921789 1.493995 -3.192363
[1] -2.1023524 1.0911300 0.4935613 -0.1076302 -3.9271420 1.4016313 -3.7191495 -1.2465567 -0.1952575 3.8618488
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -1.6814269 -1.482522 7.1121757 -4.061547 -2.5088902 -2.3856225 -0.58081924 -2.6641585 1.4475821 2.9164836
[2,] -0.6905325 3.382780 -0.5004359 -1.738132 -0.6617190 -3.4053345 0.77444184 1.0807931 2.1640940 -0.6704332
[3,] 4.6761249 -3.440849 2.7808842 -2.583133 -6.3105443 1.7403037 -1.61493772 1.2662698 -1.5233769 0.4415908
[4,] 0.2115252 4.443056 -1.7044552 2.918035 -5.0034226 1.5541277 -3.53718843 -2.2296928 -0.1941262 -0.6774140
[5,] 0.3878632 2.748574 0.6752702 1.857437 -3.2938886 0.6230563 2.70194227 0.5406639 3.9063635 1.7499817
[6,] 5.1451950 1.005393 3.3959577 4.156337 -4.9968636 -1.4502206 -0.04874864 2.8972701 -0.6559106 2.9110134
[7,] 1.3827486 1.724026 4.1414446 -4.461811 -0.1485019 -1.1568202 0.53589147 1.0827109 3.6963324 0.1417097
[8,] -3.7951837 0.610859 -0.6984102 1.917578 4.6779088 -1.3942097 1.80572706 -1.4748768 4.4850740 -1.2382027
[9,] -2.0605586 -1.341124 -4.8019300 1.124205 -1.2138718 3.6861666 0.78340314 0.6064607 -0.4918906 -1.2465868
[10,] -1.3369859 -1.030578 -0.8951075 1.110564 2.3580783 -0.2023702 0.91219698 -0.7711647 -4.1529154 1.5946641
What I noted from the outcome above, is that each print of the loop corresponds to every second column in the matrix, which contains all draws. I can't figure out why this happens, and I really hope that someone can point out my mistake.
Aucun commentaire:
Enregistrer un commentaire