mardi 4 janvier 2022

First two values in .Random.seed are always the same with different set.seed()s

Preamble

I've looked through other questions (1, 2, 3) describing the use and function of set.seed() and .Random.seed and can't find this particular issue documented so here it is as a question:

Inital Observation

When I inspect the .Random.seeds generated as a result of set.seed(1) and set.seed(2), I find that the first two elements are always the same (10403 & 624) while the rest appears not to be. See example below.

My questions

  1. Is that expected?
  2. Why does it happen?
  3. Will this have any untoward consequenses for any random simulation I might do based on it?

Reproducible Example

f <- function(s1, s2){
  
  set.seed(s1)
  r1 <- .Random.seed
  set.seed(s2)
  r2 <- .Random.seed
  
  print(r1[1:3])
  print(r2[1:3])
  
  plot(r1, r2)
  
}

f(1, 2)
#> [1]      10403        624 -169270483
#> [1]       10403         624 -1619336578

Created on 2022-01-04 by the reprex package (v2.0.1)

Note that the first two elements of each .Random.seed are identical but the remainder is not. You can see in the scatterplot that it's just a random cloud as expected.




Aucun commentaire:

Enregistrer un commentaire