Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

The Minimal Standard Random Number Generator

A great deal of research has gone into the question of finding an appropriate set of parameters to use in Lehmer's algorithm. A good generator has the following characteristics:

The choice of modulus depends on the arithmetic precision used to implement the algorithm. A signed 32-bit integer can represent values between tex2html_wrap_inline69363 and tex2html_wrap_inline69365. Fortunately, the quantity tex2html_wrap_inline69367 is a prime number!gif Therefore, it is an excellent choice for the modulus m.

Because Equation gif is slightly simpler than Equation gif, we choose to implement a multiplicative congruential generator (c=0). The choice of a suitable multiplier is more difficult. However, a popular choice is tex2html_wrap_inline69375 because it satisfies all three criteria given above: It results in a full period random number generator; the generated sequence passes a wide variety of statistical tests for randomness; and it is possible to compute Equation gif using 32-bit arithmetic without overflow.

The algorithm is derived as follows: First, let tex2html_wrap_inline69377 and tex2html_wrap_inline69379.gif In this case, tex2html_wrap_inline69385, tex2html_wrap_inline69387 and r<q.

Next, we rewrite Equation gif as follows:

eqnarray33982

This somewhat complicated formula can be simplified if we let tex2html_wrap_inline69391:

eqnarray33985

Finally, we make use of the fact that m=aq-r to get

  equation33988

Equation gif has several nice properties: Both tex2html_wrap_inline69395 and tex2html_wrap_inline69397 are positive integers between 0 and m-1. Therefore the difference tex2html_wrap_inline69403 can be represented using a signed 32-bit integer without overflow. Finally, tex2html_wrap_inline69405 is either a zero or a one. Specifically, it is zero when the sum of the first two terms in Equation gif is negative and it is one when the sum is positive. As a result, it is not necessary to compute tex2html_wrap_inline69405--a simple test suffices to determine whether the third term is 0 or m.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.