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_inline68364 and tex2html_wrap_inline68366. Fortunately, the quantity tex2html_wrap_inline68368 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_inline68376 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_inline68378 and tex2html_wrap_inline68380.gif In this case, tex2html_wrap_inline68386, tex2html_wrap_inline68388, and r<q.

Next, we rewrite Equation gif as follows:

eqnarray33770

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

eqnarray33773

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

  equation33776

Equation gif has several nice properties: Both tex2html_wrap_inline68396 and tex2html_wrap_inline68398 are positive integers between 0 and m-1. Therefore the difference tex2html_wrap_inline68404 can be represented using a signed 32-bit integer without overflow. Finally, tex2html_wrap_inline68406 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_inline68406--a simple test suffices to determine whether the third term is 0 or m.


next up previous contents index

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