Next: RNG seeding, Up: Random Number Generators [Contents][Index]
These are the basic random number generators (RNGs):
Uniform
Uniform reals on [0,1)
Normal
Normal with specified mean and variance
Exponential
Exponential with specified mean
DiscreteUniform
Integers uniformly distributed over a specified range.
Beta
Beta distribution
Gamma
Gamma distribution
F
F distribution
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(0.5,0.25); // mean = 0.5, std dev = 0.25 double x = normalGen.random(); // x is a normal random number }
The generators which Blitz++ provides are not suitable for parallel programs. If you need parallel RNGs, you may find http://sprng.cs.fsu.edu (the Scalable Parallel Random Number Generators Library) useful.
Next: RNG seeding, Up: Random Number Generators [Contents][Index]