lundi 18 juin 2018

Unexpected behavior of set.seed() in conjunction with .Random.seed in R?

I stumbled across this unexpected behavior (to me), which is likely due to my misuse and/or misunderstanding of .Random.seed.

set.seed(1234)  # This is for reproducibility
s1 <- .Random.seed
set.seed(s1)
s2 <- .Random.seed
runif(1)
s3 <- .Random.seed
set.seed(s3)
s4 <- .Random.seed
all(s2==s4)  # TRUE

The part I don't understand is the last line. How can the seed set have "returned" to s2, that is, to the set before runif(1) was called?

I understand that .Random.seed saves the seed set for the uniform random-number generator, but it was my understanding that I could also use it as an input for the set.seed() function.

I would like to store a bunch of such seed sets for later use. A workaround is to store integers instead (for instance 1234, 1235, etc...) but I somehow liked the idea to let the RNG decide what the next sate should be.




Aucun commentaire:

Enregistrer un commentaire