[ThinAir Home] [Table of Contents]

10 Bad Pseudo-Random Number Generators

PRNGs that produce non-random sequencies are useful for testing. Although these generators do not produce pseudo-random number sequences, they have the same interface as the other classes in the ThinAir library. This allows them to be used in worst-case scenarios and in testing random number generator test code. In this section, I also include some notoriously bad generators that have been proposed, and used, in the past.

10.1 ConstantRandGen

The ConstantRandGen takes a single argument when constructed. Every time the Number() member function is called, ConstantRandGen returns that argument. The LimitedNumber() member function returns the modulus of its value with the limit argument. This generator is derived from RandomGenerator and is implemented in randnot.h and randnot.cpp. The constructor for this class has the following prototype:

explicit ConstantRandGen( size_rand seed )

Uses its seed argument as its output value.

10.2 CounterRandGen

The CounterRandGen takes two arguments when constructed, a starting value and an increment. The behavior of the CounterRandGen class is defined be the equation

Xn = (Xn-1 + incr) mod 232.

This generator is derived from LinConRandGen and is implemented in randnot.h and randnot.cpp. The constructor for this class has the following prototype:

explicit CounterRandGen( size_rand seed, size_rand incr )

The seed argument supplies the initial value of the generator. The incr argument is added to the current value when a new value is required. The default value for seed is 0. The default value for incr is 1.

10.3 SawtoothRandGen

The SawtoothRandGen generator counts from 0 to limit and then resets to 0. This generator is derived from LinConModRandGen and is implemented in randnot.h and randnot.cpp. The constructor for this class has the following prototype:

explicit SawtoothRandGen( size_rand limit )

Takes a single limit argument when constructed.

10.4 SquareRandGen

The SquareRandGen class implements a generator that oscillates between two values. When constructing an object of this class, you specify the two numbers and the number of times each is output in a row. It is derived from RandomGenerator and is implemented in randnot.h and randnot.cpp. The constructor for this class has the following prototype:

SquareRandGen( size_rand first, size_rand second, size_rand dura )

The first argument and the second argument is each returned by the generator. The dura argument defines how many of each value the generator returns before swapping values.

10.5 RANDURandGen

The RANDURandGen class implements a generator that has been implemented widely. In spite of a long period, you should avoid this generator because it does not generate uncorrelated numbers. The RANDURandGen class is derived from MultConModRandGen and is implemented in erndmcnm.h. Its parameters are described in [4, page 104]. The constructor for this class has the following prototype.

explicit RANDURandGen( size_rand seed )

The seed argument defines the initial value for the generator. The default value for seed is 1.

This generator is often cited as a bad example simply because it has been so widely used. Although the generator is not a good one, RANDU was the standard PRNG for many math libraries and systems for years.

10.6 FibonacciRandGen

The FibonacciRandGen class implements an extremely bad example of a PRNG. In [4, page 26], this generator is described as having a period greater than the modulus. However, it is fairly obvious that the numbers returned by this generator do not appear very random. The FibonacciRandGen class is derived from SecConModRandGen and is implemented in randnot.h. The constructor for this class has the following prototype:

explicit FibonacciRandGen( unsigned long mod, size_rand seed )

The mod argument sets the modulus. The seed value sets the initial value of the generator. The default value for seed is 1.


For further information, contact G. Wade Johnson (gwadej@anomaly.org).

© 1997 G. Wade Johnson. All rights reserved.
http://www.anomaly.org/ThinAir/badprngs.html