I found what I consider unstable behavior (but for which I hope there is a simple explanation) in R using seeds in combination with rbinom() when prob=0.5 . General idea. For me, if I set the seed, run rbinom() once (i.e., execute one random process), although prob , the random seed should change by one step. Then, if I set the seed to the same value again and start another random process (for example, rbinom() again, but possibly with a different prob value), the seed should change again to the same value as for the previous one single random process.
I found that R does exactly this while I use rbinom() with any prob!=0.5 . Here is an example:
Compare the semester vector, .Random.seed , for two probabilities other than 0.5:
set.seed(234908) x <- rbinom(n=1,size=60,prob=0.4) temp1 <- .Random.seed set.seed(234908) x <- rbinom(n=1,size=60,prob=0.3) temp2 <- .Random.seed any(temp1!=temp2) > [1] FALSE
Compare sample vector, .Random.seed , for prob = 0.5 vs. prob! = 0.5:
set.seed(234908) x <- rbinom(n=1,size=60,prob=0.5) temp1 <- .Random.seed set.seed(234908) x <- rbinom(n=1,size=60,prob=0.3) temp2 <- .Random.seed any(temp1!=temp2) > [1] TRUE temp1==temp2 > [1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE > [8] TRUE TRUE TRUE TRUE TRUE TRUE TRUE ...
I found this for all comparisons prob=0.5 against all other probabilities in the set {0,1, 0,2, ..., 0,9}. Similarly, if I compare any prob values ββwith {0.1, 0.2, ..., 0.9} except 0.5, the .Random.seed vector .Random.seed always incremental. These facts are true for odd or even size within rbinom() .
To make it even weirder (I'm sorry this is a bit confusing - this has to do with how my function is written) when I use probabilities stored as elements in a vector, I have the same problem if 0.5 is the first element, but not the second. Here is an example for this case:
First case: 0.5 - the first probability the vector refers to
set.seed(234908) MNAR <- c(0.5,0.3) x <- rbinom(n=1,size=60,prob=MNAR[1]) y <- rbinom(n=1,size=50,prob=MNAR[2]) temp1 <- .Random.seed set.seed(234908) MNAR <- c(0.1,0.3) x <- rbinom(n=1,size=60,prob=MNAR[1]) y <- rbinom(n=1,size=50,prob=MNAR[2]) temp2 <- .Random.seed any(temp1!=temp2) > [1] TRUE any(temp1!=temp2) > [1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE > [8] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
The second case: 0.5 - the second probability indicated in the vector
set.seed(234908) MNAR <- c(0.3,0.5) x <- rbinom(n=1,size=60,prob=MNAR[1]) y <- rbinom(n=1,size=50,prob=MNAR[2]) temp1 <- .Random.seed set.seed(234908) MNAR <- c(0.1,0.3) x <- rbinom(n=1,size=60,prob=MNAR[1]) y <- rbinom(n=1,size=50,prob=MNAR[2]) temp2 <- .Random.seed any(temp1!=temp2) > [1] FALSE
Again, I believe that despite the values ββused for prob and size , this template is preserved. Can anyone explain this secret to me? This causes a problem because the results, which should be the same, are different, because for some reason the seed is used / calculated differently when prob=0.5 , but in no other instance.