[Rd] bug in detection of zero state for Mersenne Twister (PR#10362)

From: <savicky_at_cs.cas.cz>
Date: Mon, 22 Oct 2007 14:50:26 +0200 (CEST)

Full_Name: Petr Savicky
Version: all versions starting from 1.7.0 OS: observed on Linux, but is platform independent Submission from: (NULL) (

The function runif(n) contains a protection against the zero state of Mersenne Twister stored in .Random.seed. If the state is zero, it is regenerated using current time, since a zero state is a fixed point of the recurrence and, hence, produces a constant sequence.

The detection of zero state contains a bug, due to which a zero state may not be recognized. This happens if there are nonzero bits in .Random.seed[3] at positions, which are not used by Mersenne Twister (only the most significant bit of .Random.seed[3] is used and the remaining 31 are discarded).

An example of such a situation is
  .Random.seed[3] <- as.integer(1)
  .Random.seed[4:626] <- as.integer(0)
  x <- runif(10000)
  all(x==x[1]) # TRUE
  all(.Random.seed[3:626]==0) # TRUE
The output shows that the initial state was indeed effectively zero, since the final state is all zeros.

The following patch to FixupSeeds corrects the detection of zero state:

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 22 Oct 2007 - 12:54:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 25 Oct 2007 - 11:37:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.