OONSTD: random number generators

tveldhui (tveldhui@extreme.indiana.edu)
Thu, 11 Feb 1999 19:48:59 -0500 (EST)

Hi folks, I've been working on random number generators
lately. I've kept the RNG code separate from Blitz++
proper, in the hope that POOMA, MTL, SL++, A++/P++ etc. people
could use and contribute to the same library -- it is the sort
of thing we all need, and of course standardization is a Good Thing.

I'm attaching a sketch of the design, and there are user
documents at
http://seurat.uwaterloo.ca/blitz/manual/blitz08.html

I am flexible on the design, namespace, etc. I am floating this
as a proposal for discussion and would be happy to modify it to
suit everyone's needs. I volunteer to maintain the code and
incorporate patches if everyone is agreeable.

Design sketch:

There are two types of generators:
Integer RNGs: provide uniformly distributed, unsigned 32 bit integers.
RNGs: use Integer RNGs (IRNGs) to provide other distributions.

The default IRNG is Matsumoto and Nishimura's "Mersenne Twister"
MT19937, which has a period of 2^19937-1 (i.e. it will never repeat).
This generator has passed lots of stringent tests, including the
Diehard suite.

These RNGs are included now:
Uniform: uniform on [0,1) (the default)
UniformClosedOpen: uniform on [0,1)
UniformClosed: uniform on [0,1]
UniformOpen: uniform on (0,1)
UniformOpenClosed: uniform on (0,1]
NormalUnit: normal with zero mean, unit variance
Normal: normal with specified mean and variance
ExponentialUnit: exponential with unit variance
Exponential: exponential with specified variance
F: F distribution
Beta: Beta distribution
ChiSquare: Chi Square distribution
Gamma: Gamma distribution
DiscreteUniform Discrete uniform on 0..N-1

Others will be added as time allows.

RNGs take three template parameters (there are defaults for
all three). As an example,

Uniform<T, IRNG, stateTag>
T: type of random number to generate (float, double, or long double
for continuous distributions).
Note that double + long double are slower to generate, because filling
the entire mantissa with random bits requires several random integers.
Default is float.
IRNG: underlying Integer RNG to use. Default is MersenneTwister.
stateTag: either sharedState or independentState. If sharedState, the
IRNG is shared with other generators. If independentState,
this RNG contains its own IRNG. The default is sharedState.

RNGs have these methods:

T random(); returns a random number
void seed(unsigned int) seeds the IRNG

Plus others to set mean, variance and other parameters.