Random Number Generation

SECRandom is a utility class for generating random numbers.

An instance of SECRandom is initialized with an integer seed value from which random numbers are derived. If a seed of zero (0) is provided, a seed based on the current system time is used.

SECRandom can provide a random value within a range by specifying an upper and lower bound for the range. SECRandom can also generate a skewed random result based on an array of weights specified by the user.

Each entry of the array of weights specifies an unsigned integer value (the weight) that specifies the probability that a random number will fall in that position. A higher number increase the probability that a random number will be generated for that position. For example, consider a weighted array of 4 weights with the following values.

Position | Weight |
---|---|

0 | 1 |

1 | 7 |

2 | 0 |

3 | 2 |

The sum of the weights is 10. So there is a 10 percent chance that a random number of 0 will be generated, a 70 percent chance for a random number of 1, no chance for a number of 2, and a 20 percent chance that a value of 3 will be generated. These weights can be obtained using the GetRandomWeighted() method.

The SECRandom Class

The SECRandom class provides a method for generating a random number based on a seed value. To skew the results of the random number generation, SECRandom also supports weighted vectors.

Figure 138 – The Objective Toolkit Random Number Generator Class Hierarchy

SECRandom acts as a front end to the srand() run-time function. With SECRandom, you can set the effective range for the random integer you want to generate. You can also establish a weighted vector array to specify the distribution of the generated values.

Using SECRandom

The following sections show how to generate random numbers from uniform and weighted distributions.

To generate an unsigned random number (0 to 32767)

Create an instance of SECRandom. If you want, you can also set a seed value. Call GetRandom() to return an unsigned integer (0 to 32767). For example:

SECRandom rnd(m_nSeed);

UINT result = rnd.GetRandom();

If no seed value or a seed value of zero (0) is entered (default), SECRandom generates a seed based on the current time. This is useful if you don’t want to duplicate the sequence of random numbers for different trials.

To set the range of the random numbers generated

Call the SetUBound() and SetLBound() methods. The upper bound must always be greater than the current lower bound and the lower bound must be less than the current upper bound. An example of initializing an instance of SECRandom and setting the desired result range from 100 to 500 is shown below:

void GenerateRandom()

{

// Initialize with seed value of 50

SECRandom *pRandom = new SECRandom(50);

// Set a range of 100 to 500.

pRandom->SetLBound(100);

pRandom->SetUBound(500);

// retrieve a random value in the desired range

UINT result = pRandom->GetRange();

delete pRandom;

}

You can use an overload of GetRange() to set the range and return a value in one step.

To generate weighted random values

You can assign a weighted set of vectors to an SECRandom instance to change the distribution of the random values. For example, in a given range of 0 to 9, you might want to place a 70 percent chance of generating a result of 5, a 20 percent chance of generating a result of 2, and a 10 percent chance of generating a 1.

1. Instantiate an object of the SECRandom class. For example:

SECRandom rnd(nSeed);

2. Initialize a weighted vector array in SECRandom with the InitWeights() method.

// Initialize 10 entries in the weighted vector list

pRandom->InitWeights(10);

3. Assign the desired percentages with the AddWeight() method.

// Assign the weights for the probabilities:

// 10% to generate a 1

// 20% to generate a 2

// 70% to generate a 5.

pRandom->AddWeight(1,10);

pRandom->AddWeight(2, 20);

pRandom->AddWeight(5, 70);

4. To retrieve a weighted vector result, call the GetRandomWeighted() method.

// Retrieve the result

pRandom->GetRandomWeighted();

Key SECRandom Methods

Here is an overview of some of the key SECRandom methods:

SetBounds(). Sets the range of the random number to be generated.

AddWeight(). Adds an entry to the weighted vector array.

InitWeights(). Initializes the weighted vector array. (Specifies the number of weights to use.)

GetRandom(). Returns a random number between 0 and 32767, inclusive.

GetRandomWeighted(). Returns a weighted random number based on the current weight vector.

GetRange(). Returns a random number within the current range.

GetSeed(). Retrieves the current seed value from which random numbers are generated.

SECRandom Sample

The Objective Toolkit randemo sample (Samples\Toolkit\MFC\Utility\randemo) illustrates how to use SECRandom. This sample does not ship with the product. For information on how to obtain this sample, see “Location of Sample Code” in the Getting Started part.