Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
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_inline68081 and tex2html_wrap_inline68083. Fortunately, the quantity tex2html_wrap_inline68085 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_inline68093 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_inline68095 and tex2html_wrap_inline68097.gif In this case, tex2html_wrap_inline68103, tex2html_wrap_inline68105, and r<q.

Next, we rewrite Equation gif as follows:

eqnarray33522

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

eqnarray33525

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

  equation33528

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


next up previous contents index

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