Great Care is Needed

In response to my "Invitation to Flip me off," Jeffrey Sax pointed out that the Multiplicative Congruential (MC) Pseudo-Random Number Generator (PRNG) z <- (a*z)mod p has better statistics than I had thought (see feedback to the blog entry, and a=16807, p=2^31-1). Of course (!) this MC generates 31 bits, not 32 bits of output each cycle, and those bits are evenly distributed. The 48 to 52 ratio of 0's to 1's arises from counting bits in 32-bit registers, rather than in 31-bit registers. The expected number of 0 bits in a 31-bit register is 15.5, and miscounting them in a 32-bit register results in a ratio of 15.5 / 32, which is about 48 pct. Point scored for Jeffrey! The C# System.Random behaves equally better. Both also score a cumulative 21.3 on ComScire's tests when scaled up to 32 bits via double precision as follows: (z * (2^32-1) / (2^31-2)).  This is not nearly as good as Marsaglia's or Mersenne Twister's 24+ scores, but it is not completely hopeless. HOWEVER, it does demonstrate that the integer output of MC and System.Random cannot be used without conditioning & full understanding: even a non-naive, careful examination like mine can be fooled by the apparent but untrue.

A small mystery remains. The scaled-up MC and System.Random numbers still have a bit ratio of .4947, where as Marsaglia and Mersenne are 0.5000. Mathematica code is in the feedback of "Invitation..." for anyone who would like to play around. Ideas, anyone?

Published Monday, August 30, 2004 3:48 PM by brianbec

Comments

# re: Great Care is Needed@ Tuesday, August 31, 2004 12:27 PM

For the multiplicative-congruential RNG, the bias can be explained as follows:

1/(2^31-2)=(1/2)*(1/(2^30-1))=(1/2^31 + 1/2^61 + ...)
Therefore, (2^32-1)*p/(2^31-2) = ... = 2p - 3p/2^31.

Because p<2^31, the last bit of this number is 1 roughly 2/3 of the time, giving a ratio of 0.5104/0.4896. This is well within the margin of error of your Mathematica result.

Jeffrey Sax

# re: Great Care is Needed@ Tuesday, August 31, 2004 12:48 PM

Very good. Another point scored for Jeffrey :)

brianbec

Leave a Comment

(required) 
(required) 
(optional)
(required)