[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These are the basic random number generators (RNGs):
Uniform
Normal
Exponential
DiscreteUniform
Beta
Gamma
F
To use these generators, you need to include some subset of these headers:
#include <random/uniform.h> #include <random/normal.h> #include <random/exponential.h> #include <random/discrete-uniform.h> #include <random/beta.h> #include <random/gamma.h> #include <random/chisquare.h> #include <random/F.h> using namespace ranlib; |
All the generators are inside the namespace ranlib,
so a using namespace ranlib directive is required (alternately, you
can write e.g. ranlib::Uniform<>
).
These generators are all class templates. The first template parameter is
the number type you want to generate: float, double or long double for
continuous distributions, and integer for discrete distributions. This
parameter defaults to float
for continuous distributions,
and unsigned int
for discrete distributions.
The constructors are:
Uniform(); Normal(T mean, T standardDeviation); Exponential(T mean); DiscreteUniform(T n); // range is 0 .. n-1 Beta(T a, T b); Gamma(T mean); ChiSquare(T df); F(T dfn, T dfd); |
where T
is the first template parameter (float
, double
,
or long double
). To obtain a random number, use the method
random()
. Here is an example of constructing and using a
Normal
generator:
#include <random/normal.h> using namespace ranlib; void foo() { Normal<double> normalGen; double x = normalGen.random(); // x is a normal random number } |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The generators which Blitz++ provides are not suitable for parallel programs. If you need parallel RNGs, you may find http://www.ncsa.uiuc.edu/Apps/SPRNG useful.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You may seed a random number generator using the member function
seed(unsigned int)
.
By default, all random
number generators share the same underlying integer random number generator.
So seeding one generator will seed them all. (Note: you can create
generators with their own internal state; see the sections below). You
should generally only seed a random number generator once, at the beginning
of a program run.
Here is an example of seeding with the system clock:
#include <random/uniform.h> #include <time.h> using namespace ranlib; int main() { // At start of program, seed with the system time so we get // a different stream of random numbers each run. Uniform<float> x; x.seed((unsigned int)time(0)); // Rest of program ... } |
Note: you may be tempted to seed the random number generator from a static initializer. Don't do it! Due to an oddity of C++, there is no guarantee on the order of static initialization when templates are involved. Hence, you may seed the RNG before its constructor is invoked, in which case your program will crash. If you don't know what a static initializer is, don't worry -- you're safe!
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
There are really two types of RNGs:
Integer
RNGs
By default, the Integer RNG used is a faithful adaptation of the Mersenne
Twister MT19937
@cindex MersenneTwister due to Matsumoto and
Nishimura (see ACM Transactions on Modeling and Computer Simulation,
Vol. 8, No. 1, January 1998, pp 3-30,
http://www.math.keio.ac.jp/~matumoto/emt.html,
http://www.acm.org/pubs/citations/journals/tomacs/1998-8-1/p3-matsumoto/).
This generator has a period of 2^{19937-1}, passed several stringent
statistical tests (including the
http://stat.fsu.edu/~geo/diehard.html tests), and has speed
comparable to other modern generators.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
RNGs take three template parameters, all of which have default values.
Using the Uniform
RNG as an example, the template parameters of
Uniform<T, IRNG, stateTag>
are:
T
float
,
double
, or long double
for continuous distributions; an
integer type for discrete distributions). Note that generating double and
long double RNGs takes longer, because filling the entire mantissa with
random bits requires several random integers. The default parameter for
most generators is float
.
IRNG
stateTag
sharedState
or independentState
. If
sharedState
, the IRNG is shared with other generators. If
independentState
, the RNG contains its own IRNG. The default is
sharedState.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
RNGs have these methods:
T random(); |
void seed(unsigned int); |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To save space in the below list, template parameters have been omitted and only constructors are listed. The notation [a,b] means an interval which includes the endpoints a and b; (a,b) is an interval which does not include the endpoints.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Uniform<>() |
UniformClosedOpen<>() |
Uniform<>
.
UniformClosed<>() |
UniformOpen<>() |
UniformOpenClosed<>() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
NormalUnit<>() |
Normal<>(T mean, T standardDeviation) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ExponentialUnit<>() |
Exponential<>(T mean) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Beta<>(T a, T b) |
setParameters(T a, T b)
to change the parameters.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ChiSquare<>(T df) |
df
degrees of freedom. The parameter
df must be positive. Use the method setDF(T df)
to change the
degrees of freedom.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Gamma<>(T mean) |
setMean(T mean)
to
change the mean.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
F<>(T numeratorDF, T denominatorDF) |
setDF(T dfn, T dfd)
to change the
degrees of freedom.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
DiscreteUniform<>(T n) |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |