Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Objective Toolkit User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

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

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

16.6.2 Using SECRandom

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

16.6.2.1 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:

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.

16.6.2.2 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:

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

16.6.2.3 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:

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

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

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

16.6.2.4 Key SECRandom Methods

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

16.6.3 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 Section 3.6.1, "Location of Sample Code," in the Stingray Studio Getting Started Guide.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.