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

Implementation

We now describe the implementation of a random number generator based on Equation gif. Program gif defines the RandomNumberGenerator class. This class has only static members . Because there can only be one instance of a static data member, the implementation of the RandomNumberGenerator class follows the singleton  design pattern.

   program34003
Program: RandomNumberGenerator Class Definition

Program gif gives the implementation of the RandomNumberGenerator class. The SetSeed function is used to specify the initial seed, tex2html_wrap_inline69291. The seed must fall between 0 and m-1. If it does not, an exception is thrown.

   program34017
Program: RandomNumberGenerator ClassSetSeed and Next Member Function Definitions

The Next function generates the elements of the random sequence. Each subsequent call returns the next element of the sequence. The implementation follows directly from Equation gif. Notice that the return value is normalized. Therefore, the values computed by the Next function are uniformly distributed on the interval (0,1).


next up previous contents index

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