// park-miller.cc Park-Miller random number generator
// W. Langdon cs.ucl.ac.uk 5 May 1994
// W.B.Langdon@cs.bham.ac.uk 12 December 1996 port for alpha
#include
#include "park-miller.h"
/*********************************************************************
INTRODUCTION
This file contains (at your own risk) an implementation of the
Park-Miller minimum random number generator (Comm ACM Oct 1988
p1192-1201, Vol 31 Num 10). It is suitable for use with GPQUICK-2.1
USAGE
All psuedo random number generators need a seed (ie initial starting
value from which to generate the sequence). In some cases this is
based on reading the current system time. This code allows you to
specify what it is. Any positive 32 bit number (NOT zero!) will do the
first time you call it. Use the 32 bit value it returns as the seed
for the next call, and so on for each sucessive call.
A positive psuedo random integer uniformly distributed between 1 and
2147483647 (ie 2**31-1) is returned.
Example
Taking the modulus 52 of the returned value will give you a value
between 0 and 51. This has a slight bias as 2**31-1 is not exactly
divisible by 52 but it may be good enough for your purposes.
NB use the whole 32 bit random value as the seed for the next call. Do
not use its modulus, as this will lead to a very short random number
sequence not the full 2147483647 which park-miller provides.
*********************************************************************/
int intrnd (int& seed) // 1<=seed<=m
{
#if LONG_MAX > (16807*2147483647)
int const a = 16807; //ie 7**5
int const m = 2147483647; //ie 2**31-1
seed = (long(seed * a))%m;
return seed;
#else
double const a = 16807; //ie 7**5
double const m = 2147483647; //ie 2**31-1
double temp = seed * a;
seed = (int) (temp - m * floor ( temp / m ));
return seed;
#endif
}