35bits random generator
Peter Desnoyers
desnoyer at Apple.COM
Fri Feb 10 05:08:02 AEST 1989
If anyone needs a reliable random number generator, this is the
generator used in SIMSCRIPT II. It is a linear congruential generator,
and passes various tests for goodness. Just remember that you can use
multiple seeds - NEVER do something like
if (random(seed) < 0.00001) /* discrete time Poisson arrivals */
packet_size = (MAX_SIZE) * random(seed); /* same seed */
Use two different seeds instead. (Why is this bad? Why isn't
mean(packet_size) equal to MAX_SIZE/2? These questions are left for
the reader.)
Peter Desnoyers
--- code follows ---
/*****************************************************************************
* file: rnd.c *
* description: implementation of random number generator used in *
* SIMSCRIPT II. provides uniform and exponentially distributed *
* numbers. *
* Peter Desnoyers - 7/28/87 *
*****************************************************************************/
/*** INCLUDES ***/
#include <math.h>
/*** DEFINES ***/
#define MODULUS 2147483647
/*** EXTERNALS ***/
extern double log();
/*****************************************************************************
* function: random_u *
* arguments: int * seed *
* returns: double *
* value: random number uniformly distributed over (0,1) *
*****************************************************************************/
double
random_u( seed)
int * seed;
{
* seed = ((unsigned)(*seed * 1078318381)) % MODULUS;
return ((double) *seed / MODULUS);
}
/*****************************************************************************
* function: random_e *
* arguments: int * seed *
* returns: double *
* value: exponentially distributed random variable with mean 1 *
*****************************************************************************/
double
random_e( seed)
int * seed;
{
return (-1.0 * log( random_u( seed)));
}
More information about the Comp.lang.c
mailing list