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 properties. 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.

   program33791
Program: RandomNumberGenerator class.

The Seed property provides a set accessor to specify the initial seed, tex2html_wrap_inline68292. The seed must fall between 0 and m-1. If it does not, an exception is thrown. The Seed property also provides a get accessor that returns the current seed value.

The Next property generates the elements of the random sequence. Each subsequent call to its get accessor 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 accessor are uniformly distributed on the interval (0,1).


next up previous contents index

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