Random Number Generators |
![]()
where m is the modulus, a and c are positive integers called the multiplier and increment respectively. X0 is an initial number known as seed. The above recurrence eventually repeats itself with a period not greater than m. If m, a and c are properly chosen, then this period is of maximal length (i.e. of length m).
(6.2.1.1)
can be as good as any of the more general linear
congruential generators that have
,
if the multiplier a and modulus m are chosen very carefully.
We use an algorithm proposed by Park and Miller [6] called Minimal
Standard Generator based on the choices,
(6.2.1.2)
![]()
is
called the Jacobian determinant.
![]()
the so-called standard normal distribution is given by
taking 0 mean and 1 variance. An arbitrary normal distribution can be
converted to a standard normal distribution by changing variables to
,
so
yielding,
![]()
![]()
![]()
and ![]()
![]()
![]()
or ![]()
![]()
;
0 otherwise
0
otherwise
where
is the mean.
![]()
where a is the order.
![]()
= 0 ,otherwise
where n is the number of trials and p is the probability of success.
In the above,
where
![]()
We can now compute the mean as np and variance as np(1-p).
Math<Toolkit> provides a set of classes
to generate randoms numbers in various distribution. The classes provided are
os_num_rand_uniform
uniform random number generator
os_num_rand_normal
normal random number generator
os_num_rand_poisson
poisson random number generator
os_num_rand_gamma
gamma random number generator
os_num_rand_exponential exponential random number generator
os_num_rand_binomial binomial random
number generator
The example code below shows the usage of the above classes to generate random numbers.
#include <ospace/math.h>&
int
main()
{
os_math_toolkit math_init;
int i, NUM_DEV = 5;
cout << "===== random uniform =====" << endl;
os_rand_uniform ru( 0, 10 );
cout << "lower bound = " << ru.lower_bound() << endl;
cout << "upper bound = " << ru.upper_bound() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << ru.deviate() << " ";
cout << endl;
ru.lower_bound( 10 );
ru.upper_bound( 20 );
cout << "lower bound = " << ru.lower_bound() << endl;
cout << "upper bound = " << ru.upper_bound() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << ru.deviate() << " ";
cout << "\n" << endl;
cout << "===== random normal =====" << endl;
os_rand_normal rn( 0, 10 );
cout << "mean = " << rn.mean() << endl;
cout << "variance = " << rn.variance() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rn.deviate() << " ";
cout << endl;
rn.mean( 10 );
rn.variance( 20 );
cout << "mean = " << rn.mean() << endl;
cout << "variance = " << rn.variance() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rn.deviate() << " ";
cout << "\n" << endl;
cout << "===== random poisson =====" << endl;
os_rand_poisson rp( 10 );
cout << "mean = " << rp.mean() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rp.deviate() << " ";
cout << endl;
rp.mean( 20 );
cout << "mean = " << rp.mean() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rp.deviate() << " ";
cout << "\n" << endl;
cout << "===== random exponential =====" << endl;
os_rand_exponential re( 10 );
cout << "lambda = " << re.lambda() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << re.deviate() << " ";
cout << endl;
re.lambda( 20 );
cout << "lambda = " << re.lambda() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << re.deviate() << " ";
cout << "\n" << endl;
cout << "===== random gamma =====" << endl;
os_rand_gamma rg( 10 );
cout << "order = " << rg.order() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rg.deviate() << " ";
cout << endl;
rg.order( 20 );
cout << "order = " << rg.order() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << re.deviate() << " ";
cout << "\n" << endl;
cout << "===== random binomial =====" << endl;
os_rand_binomial rb( 10, 0.33 );
cout << "trials = " << rb.trials() << endl;
cout << "probability = " << rb.probability() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rb.deviate() << " ";
cout << endl;
rb.trials( 20 );
rb.probability( 0.66 );
cout << "trials = " << rb.trials() << endl;
cout << "probability = " << rb.probability() << endl;
cout << "deviates = ";
for ( i = 0; i < NUM_DEV; i++ )
cout << rb.deviate() << " ";
cout << "\n" << endl;
return 0;
}
Copyright©1994-2013 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.