Some notes on random number generators

Mai Zhou, April 1999

1. Almost every computer language has a random number generator (RNG), eg. java has one. However their quality differ.

2. Strictly speaking, the "real" random numbers are those generated by hardware. Those generated by software (with a seed or seeds, sometimes set implicitly) can only be called "Pseudo" random number generators (PRNG).

3. Some example of the hardware RNG: (a) flip a coin or toss a dice (b) Lotto drawings every evening. Some state has a long record of the drawing outcomes. (c) Published pre-generated digits. Marsaglia publishes a CD Rom of about 600MB of RN's, also available on-line by ftp. (d) Hardware device converting heat generated electrical noise signal into RN. Like what you see on a TV screen when there is no signal.

4. The hardware RNG is slow and expensive. It is not repeatable in case (d) above [well, this may not be a bad thing]. In big simulations, you can run out of them in case (a) (b) (c).

5. Software "Pseudo" random number generator has many different type. Some are "bad" i.e. not random.

6. The basic RNG usually produce a double precision number inside [0, 1], called uniform[0,1] distribution. Other kind of RN with different distributions can often be obtained via a transformation: for example -log( uniform[0,1] ) is called exponential distributed RN.

7. Marsaglia has a "battery" of tests for testing the randomness and independentness of RNs, called "Die Hard Battery of Test". Some RNG fail some of the tests.

8. Software R (since version 0.64.0 at least) provides three types of PRNG.

  • (1) Wichmann-Hill PRNG. (use 3 seeds)
  • (2) Marsaglia's multiply-with-carry PRNG. (use 2 seeds)
  • (3) Marsaglia's Super-Duper PRNG. (use 2 seeds)

    The quality of (1) above is not as good as (2),(3). Please note the windows version of R0.62.0 has a bad PRNG. It is not in the Linux's version of R0.62.0 though. In R version 0.64.0 (or later) select the PRNG by

    > .Random.seed <- c(0, 1, 2, 3)
    here the first 0 is to select type of PRNG, this case the Wichmann-Hill PRNG. The following 1 2 3 is the three integer seeds needed for any three PRNG.

    > .Random.seed <- c(1, 1, 2, 3)
    set the PRNG to Marsaglia's multiply-with-carry PRNG. Even though it need only 2 seeds, we supply 3.
    [added 8/2000]Please note for R version 1.0.0 and later, there is a function set.seed( ) used to set seed just like in Splus.

    To check which kind of PRNG is in use inside R,
    > RNGkind( )

    Software Splus provides only one type of PRNG. In earlier version (version 3.x), it provides Marsaglia's Super-Duper PRNG. In later versions (4.x), it provides a modified Marsaglia's Super-Duper PRNG which skips those RN that is identically 0.

    In Splus there is a function set.seed( )

    > set.seed(135)

    this sets the seed for (modified) Super-Duper PRNG. Or we can also work on the .Random.seed directly.

    9. Examples of PRNG:

  • (1) In C, the multiply-with-carry PRNG suggested by Marsaglia. This is the second type of PRNG in R0.64.0.
  • (2) In Splus, the Wichmann-Hill PRNG. Mainly used for cross checking with R.

    10. Possible future projects:

  • (1) test the java's PRNG using the "Die Hard Battery".
  • (2) implement Marsaglia's multiply-with-carry PRNG in Splus.

    11. Further readings and references: A compressed ps file. Hardware RNG . Diehard Battery of tests.