Math.h++ 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 Math.h++ 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 Math.h++ 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 Math.h++ 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 Math.h++ 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. Math.h++ provides the general purpose uniform (0,1) generator, RWRandGenerator, which may be used as the template parameter in all the random number classes. The Class Reference contains details on class RWRandGenerator. Section 9.10, "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 5:.
Class | Include file | Distribution |
RWTRandBinomial<T> |
<rw/rand.h> |
Binomial |
RWTRandExponential<T> |
<rw/rand.h> |
Exponential |
RWTRandGamma<T> |
<rw/rand.h> |
Gamma |
RWTRandNormal<T> |
<rw/rand.h> |
Normal (Gaussian) |
RWTRandPoisson<T> |
<rw/rand.h> |
Poisson |
RWTRandUniform<T> |
<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.h>
main() { RWTRandUniform<RWRandGenerator> ru; RWMathVec<double> v( 10, ru );
cout << v << "\n" }>
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.