Just include the header in your module that uses the random number generator and link the compiled source file into your project. Call initRand() to initialise the generator and then use either random() to return a random double between 0 and 1 or random(low,high) to obtain a random integer between high and low.
NOTE: This code has not been optimised but is rather written for clarity of the algorithm. Feel free to optimise it to your hearts delight.
Here's the header file...
#ifndef RAND_H
#define RAND_H
void initRand(void);
void advanceRand(void);
double random(void);
int random(int low, int high);
#endif
..and here's the source file.
/****************************************************************************
* Module RAND.CPP *
* Based on an algorithm by Donald Knuth *
* (c) 2000 Tim Wilkin. *
* Permission is hereby given to use this as you see fit. I ask only that you *
* keep this header information intact. *
****************************************************************************/
#include <time.h>
#include "rand.h"
#include <math.h>
#include <iostream>
#include <fstream>
const int RAND_ARRAY_LEN=55; // Length of array of stored random numbers
const int SPECIAL_INDEX=24; // Just what the name suggests!
static double oldRand[RAND_ARRAY_LEN]; // Array of random numbers
static unsigned int index2Rand = 0; // Index into array of randoms
/**********************************************
* Function: initRand. Initialise the random number generator *
**********************************************/
void initRand(void)
{
long totalseconds = time(NULL);
int seconds = static_cast<int>(totalseconds%60L);
double seed = 0.91999/ seconds;
double newRand = 1.0e-9;
double prevRand = seed;
oldRand[RAND_ARRAY_LEN-1] = prevRand;
for (unsigned int i=0; i<RAND_ARRAY_LEN; i++)
{
unsigned int j = (21*i) % RAND_ARRAY_LEN;
oldRand[j] = newRand;
newRand = prevRand - newRand;
if (newRand < 0.0)
newRand += 1.0;
prevRand = oldRand[j];
}
advanceRand();
advanceRand();
advanceRand();
index2Rand = 0;
}
/**********************************************************************
* Function: advanceRand. Function to generate new random numbers once array is used up *
**********************************************************************/
void advanceRand(void)
{
double newRand;
for (unsigned int j=0; j<SPECIAL_INDEX; j++)
{
newRand = oldRand[j]-oldRand[j+(RAND_ARRAY_LEN-SPECIAL_INDEX)];
if (newRand < 0.0)
newRand += 1.0;
oldRand[j] = newRand;
}
for (unsigned int j=SPECIAL_INDEX; j<RAND_ARRAY_LEN; j++)
{
newRand = oldRand[j] - oldRand[j-SPECIAL_INDEX];
if (newRand < 0.0)
newRand += 1.0;
oldRand[j] = newRand;
}
}
/*******************************************************************
* Function: random(void). Generate a double precision random number from the set [0,1] *
*******************************************************************/
double random(void)
{
index2Rand++;
if (index2Rand >= RAND_ARRAY_LEN)
{
index2Rand = 0;
advanceRand();
}
return oldRand[index2Rand];
}
/********************************************************************
* Function: random(low,high). Generate an integer random number from the set [low,high] *
********************************************************************/
int random(int low, int high)
{
if (low>=high)
return low;
int tempRand = floor(random()*(double)(high-low+1) + (double)low);
if (tempRand > high)
tempRand = high;
return tempRand;
}
Questions can be directed to me at taw@csse.monash.edu.au
Cheers,
Timkin
Edited by - Timkin on August 28, 2001 1:05:48 PM