Random numbers

Header file random.h

Classes of random number generator

Rather than having a single static random number generator, this class library provides classes of generators instead. This makes it easier to parallelise calculations using random numbers, e.g. large Monte-Carlo simulations.

In order to use a random number generating class, an object must be declared in the class, and then used to supply random numbers.

A polymorphic random number generator class is also defined, Random_number_generator. If data is read to a pointer to an object of this class, a type name is first read and a random number generator of the appropriate type is initialised. If necessary, data specific to that class is then read in. To invoke the generator, use the method random() to return a double with the desired distribution. For example:

#include<random.h>
...
Random_number_generator *r;
cin >> r; // reads in type and data
...
double x = r->random(); // generates a random number
...
Subclasses include all of the generators below for continuous distributions (i.e. ignoring the `computer dartboard' discrete generator. The identifying names are also given below.

Subtractive algorithm

Generates random numbers distributed evenly over [0,1) using Knuth's subtractive algorithm. In common with most pseudo-random number generators, this routine requires an initial `seed' to define the sequence of random-appearing numbers which are produced. It is written so that it includes its own default seed. Rigorous numerical studies of the behaviour of systems which involve random numbers require control over the seed, e.g. so you can study the effect of using a different sequence of numbers. A method is provided so the user can supply another seed.

Definition
class Ransub
generate a random number
double random()
generate a random integer
integer random(
   const int i0, // minimum
   const int i1 // maximum
)
reseed generator
void reseed(const double seednew)
type name
unit_uniform

e.g.

Ransub gen; // create a generator
for (int i=0;i<1000;i++) cout << gen.random() << endl; // use it

Random numbers with general distributions

All these routines are based on the random number generator with uniform distribution, ransub. The documentation on this routine includes some important notes which are therefore relevant to all the other routines.

In all cases, a random number is generated by invoking the method random().

Uniform

class Ranuniform, uniform distribution between specified limits, type name lorentzian, data vmin, vmax (limits of range).

Lorentzian

class Ranlorentz, distribution f(x)=1 / {pi r [1 + (x-xc)2/r2]}, type name lorentzian, data r, xc (width and centre).

Gaussian (unit deviance, centred on origin)

class Rangauss, distribution f(x)=exp(-x2)/sqrt(2 pi) using Box-Muller method. type name unit_gaussian,

Gaussian (specified centre and deviance)

class Rangaussgen, distribution f(x)=exp(-(x-xc)2/r2)/sqrt(2 pi) r, type name gaussian, data r, xc (width and centre).

Exponential (specified centre and deviance)

class Ranexp, distribution f(x)=exp(-|x-xc|/r)/2 r, type name exponential, data r, xc (width and centre).

Exponential (unit width, positive arm)

class Randisexp, distribution f(x)=exp(-x) : x >= 0. Type name unit_positive_exponential,

`Computer dartboard'

Given set of probabilities, calculate random outcome. Returns index corresponding to outcome selected. If the probabilities add up to less than 1, a negative index may be returned, corresponding to `none of the itemised outcomes'.

Standalone function

int random_outcome(
   const array<double>& wt, // array of probabilities, adding up to 1 or less
   Ransub& gen // random number generator
)

Class

class Ranset
{
   public:

   // constituents
   array<double> wt; // array of probabilities, adding up to 1 or less
   ransub gen; // random number generator

   // determine index of random outcome
   int random_outcome()
};