Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
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 fields and methods. In addition, the constructor is declared private to prevent instantiation. Because there can only be one instance of a static field, the implementation of the RandomNumberGenerator class is an example of the singleton  design pattern.

   program33543
Program: RandomNumberGenerator class.

The setSeed method is used to specify the initial seed, tex2html_wrap_inline68009. The seed must fall between 0 and m-1. If it does not, an exception is thrown.

The nextDouble method 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 nextDouble method are uniformly distributed on the interval (0,1).


next up previous contents index

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