Random Number Generator Source Code
Random Number Generators

Steve Park

This is an ANSI C library for multi-stream random number generation. The use of this library is recommended as a replacement for the ANSI C rand() and srand() functions, particularly in simulation applications where the statistical 'goodness' of the random number generator is important. The library supplies 256 streams of random numbers; use SelectStream(s) to switch between streams indexed s = 0,1,...,255.

The generator used in this library is a so-called 'Lehmer random number generator' which returns a pseudo-random number uniformly distributed 0.0 and 1.0. The period is (m - 1) where m = 2,147,483,647 and the smallest and largest possible values are (1 / m) and 1 - (1 / m) respectively. For more details see:

"Random Number Generators: Good Ones Are Hard To Find"
Steve Park and Keith Miller
Communications of the ACM, October 1988

For more information about the libraries rngs and rvgs, or for information about the status of the related Prentice-Hall textbook Discrete-Event Simulation: A First Course by Steve Park and Larry Leemis, please contact leemis@math.wm.edu.

Random Number Generator Source Code

The bracketed links below are to ANSI C files. If you wish to save the files below rather than view them, click on the link with the SHIFT key depressed. With a Netscape browser, this will bring up a "Save As" dialog box.

Compressed File (for the four files)

You may download all of the files listed below in compressed form here. To extract files with gnu zip and tar facilities after downloading, use gtar zxvf rng.tgz.

Uniform(0,1) Random Number Generator Files

[rngs.h] [rngs.c]
We also have an ANSI C library for generating random variates from six discrete distributions
Generator Range (x) Mean Variance
Bernoulli(p) x = 0,1 p p*(1-p)
Binomial(n, p) x = 0,...,n n*p n*p*(1-p)
Equilikely(a,b)x = a,...,b(a+b)/2 ((b-a+1)*(b-a+1)-1)/12
Geometric(p) x = 0,... p/(1-p) p/((1-p)*(1-p))
Pascal(n, p) x = 0,... n*p/(1-p) n*p/((1-p)*(1-p))
Poisson(m) x = 0,... m m

and seven continuous distributions
Generator Range (x)Mean Variance
Uniform(a, b) a < x < b(a + b)/2 (b - a)*(b - a)/12
Exponential(m) x > 0 m m*m
Erlang(n, b) x > 0 n*b n*b*b
Normal(m, s) all x m s*s
Lognormal(a, b) x > 0 exp(a + 0.5*b*b) (exp(b*b) - 1) * exp(2*a + b*b)
Chisquare(n) x > 0 n 2*n
Student(n) all x 0 (n > 1) n/(n - 2) (n > 2)

These are the files for these distributions:
[rvgs.h] [rvgs.c]