Data Structures and Algorithms
with Object-Oriented Design Patterns in C#![]() ![]() ![]() ![]() ![]() |
We now describe the implementation of a random number generator
based on Equation .
Program
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.
Program: RandomNumberGenerator class.
The Seed property provides a set accessor
to specify the initial seed, .
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 .
Notice that the return value is normalized.
Therefore, the values computed by the Next accessor are uniformly
distributed on the interval (0,1).