Statistics Classes
Random Number Generators
The Essential Math Module provides random number generators for a variety of probability distributions. Each of these generators has the ability to generate uniform random deviates in the range (0,1). Uniform deviates are random numbers that lie within a specified range, with any one number in the range just as likely as any other. The Essential Math Module random number generators first generate a random uniform deviate in the range (0,1), then from this deviate derive a random number from the appropriate probability distribution. Thus, the statistical properties and performance of the Essential Math Module random number generators depend on the statistical properties and performance of the underlying uniform deviate generator.
The algorithms that generate uniform random deviates vary widely. Some are very quick, but have poor statistical properties; others have excellent statistical properties, but are slow. If a random number generator is not giving good results in a particular instance, standard practice is to try another algorithm. For this reason, the random number generator classes supplied with the Essential Math Module are parameterized by uniform generator type using the C++ template mechanism. This lets you pick the algorithm to generate the underlying random deviates in the random number classes without having to modify source code or derive your own class.
The random number generator classes in the Essential Math Module all derive from the abstract base class
RWTRand<Generator>. This base class contains and provides access to the uniform deviate generator
Generator, and declares the function call operator as a pure virtual function that returns a
double.
The following example illustrates the function call operator:
#include <rw/rand.h>
// The following function can take as its parameter any class
// derived from RWTRand<RWRandGenerator>, that is, any probability
// distribution:
needsRandomNumbers( RWTRand<RWRandGenerator>& rand)
{
.
. // Do stuff
.
// Generate a random number using the function call operator:
double randomNumber = rand();
// Restart the generator by invoking the method
// RWRandGenerator::restart(unsigned)
// on the underlying uniform deviate generator. Use the
// access method RWTRand<T>::generator to obtain a reference
// to the underlying uniform deviate generator.
( rand.generator() ).restart( 12345L);
.
.
.
}
int main()
{
.
. // Do stuff
.
RWTRandGamma<RWRandGenerator> randGamma;
RWTRandPoisson<RWRandGenerator> randPois;
needsRandomNumbers( randGamma );
needsRandomNumbers( randPois );
.
.
.
}
The template parameter to the random number classes must be a function object; that is, it must be a class that defines the function call operator to return a random deviate of type
double in the range
(0,1), or
[0,1]. In addition, the class must possess a default constructor. The Essential Math Module provides the general purpose uniform
(0,1) generator,
RWRandGenerator, which may be used as the template parameter in all the random number classes. The
SourcePro API Reference Guide contains details on class
RWRandGenerator.
Random Number Generator Example shows how to make a function object out of a function that computes a uniform random deviate. The random number generator classes are listed in
Table 2.
Table 2 – List of the random number generator classes, the include file that declares them, and the type of distribution that each returns
Class | Include file | Distribution |
---|
| <rw/randgen.h> | Uniform |
| <rw/randmcg31m1.h> | Uniform |
| <rw/randmcg59.h> | Uniform |
| <rw/randmrg32k3a.h> | Uniform |
| <rw/randmtwist.h> | Uniform |
| <rw/randr250.h> | Uniform |
| <rw/rand.h> | Binomial |
| <rw/rand.h> | Exponential |
| <rw/rand.h> | Gamma |
| <rw/rand.h> | Normal (Gaussian) |
| <rw/rand.h> | Poisson |
| <rw/rand.h> | Uniform |
Note that each class has a similar interface. The following example prints a vector of 10 uniformly distributed random deviates:
#include <rw/rand.h>
#include <rw/math/mathvec.h>
#include <iostream>
int main()
{
RWTRandUniform<RWRandGenerator> ru;
RWMathVec<double> v( 10, ru );
std::cout << v << "\n"
}