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.
class Ransub
double random()
integer random( const int i0, // minimum const int i1 // maximum )
void reseed(const double seednew)
unit_uniform
e.g.
Ransub gen; // create a generator for (int i=0;i<1000;i++) cout << gen.random() << endl; // use it
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()
.
class Ranuniform
,
uniform distribution between specified limits,
type name lorentzian
,
data vmin, vmax
(limits of range).
class Ranlorentz
,
distribution f(x)=1 / {pi r [1 + (x-xc)2/r2]},
type name lorentzian
,
data r, xc
(width and centre).
class Rangauss
,
distribution f(x)=exp(-x2)/sqrt(2 pi) using Box-Muller method.
type name unit_gaussian
,
class Rangaussgen
,
distribution f(x)=exp(-(x-xc)2/r2)/sqrt(2 pi) r,
type name gaussian
,
data r, xc
(width and centre).
class Ranexp
,
distribution f(x)=exp(-|x-xc|/r)/2 r,
type name exponential
,
data r, xc
(width and centre).
class Randisexp
,
distribution f(x)=exp(-x) : x >= 0.
Type name unit_positive_exponential
,
int random_outcome( const array<double>& wt, // array of probabilities, adding up to 1 or less Ransub& gen // random number generator )
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() };