Set.seed with R 2.15.2

I understand that using set.seed provides reproducibility, but this does not apply to the next R code in R 2.15.2 . Did I miss something?

 set.seed(12345) rnorm(5) [1] 0.5855288 0.7094660 -0.1093033 -0.4534972 0.6058875 rnorm(5) [1] -1.8179560 0.6300986 -0.2761841 -0.2841597 -0.9193220 
+7
source share
2 answers

set.seed() reinitializes the random number generator .

 set.seed(12345) rnorm(5) [1] 0.5855288 0.7094660 -0.1093033 -0.4534972 0.6058875 set.seed(12345) rnorm(5) [1] 0.5855288 0.7094660 -0.1093033 -0.4534972 0.6058875 set.seed(12345) rnorm(5) [1] 0.5855288 0.7094660 -0.1093033 -0.4534972 0.6058875 
+19
source

Any call that uses a random number generator will change the current seed, even if you manually set it using set.seed .

 set.seed(1) x <- .Random.seed # get the current seed runif(10) # uses random number generator, so changes current seed y <- .Random.seed identical(x, y) # FALSE 

As @StephanKolassa shows, you must reset to use the seed before each use of the random number generator to ensure that it uses the same one every time.

+12
source

All Articles