Listing 2: Implementation of random number generation class
/*
* random.cpp
*/
#include "random.h"
#include <math.h>
#define RANDOM_DEBUG 0 // set non-zero to compile main
#if RANDOM_DEBUG
#include <fstream.h>
#include <stdlib.h>
#endif
// Static variables
int RandomGenerator::stream_count = 0;
long int RandomGenerator::base_seed;
// Constants
const double MODULUS = 2147483647.0L; // 2^31 - 1 = 2,147,483,647
const double A_100000 = 241748845.0L; // A^100000 mod MODULUS
const double A = 62089911.0L; // From reference
RandomGenerator::RandomGenerator(void)
{ // Constructor
if (stream_count++ != 0) // increment stream counter for next
// constructor
{ // This is not the first stream, a new base seed must be
// calculated
/*
* The actual algorithm for this calculation is:
* Z(100000) = (Z(0)*A^100000)modm. Because of obvious
* overflow problems, A^100000modm was precomputed to be
* 241748845. The math behind this was (A*B)modm =
* ((Amodm)*(Bmodm))modm. The new seed is then computed
* as Z(100000) = (241748845*Z(0))modm
*/
base_seed =
(long int)fmod((double)base_seed * A_100000, MODULUS);
}
// Set the first seed value for the stream
my_start_seed = current_seed = base_seed;
}
double RandomGenerator::Rand(void)
{
current_seed =
(long int)fmod((double)current_seed * A, MODULUS);
return (double)current_seed / MODULUS;
}
//omitted: main() function to test generators full source code is
//provided on code disk and ftp site, see page 3 for details -- mb
//End of File