Table of Contents


set_xrandom, xrandom, grandom, frandom - random number generators


int init_xrandom(string init)int set_xrandom(int seed)
double xrandom(double
a, double b)
double grandom(double m, double d)
double frandom(double a,
double b, real_proc func)
double xrand(double a, double b)


init_xrandom initializes the random number generator with a supplied string init, the meaning of which is implementation dependant. For non-GSL implementation, an integer will be extracted, and set_xrandom will be called. For GSL implementations, the integer can be optionally followed by a GSL random number generator name (normally set via the GSl environment variable GSL_RNG_TYPE). Allowed names are (e.g.) ran0, ran1, ran2, ran3 (from Numerical Recipes), rand48, random_bsd, mt19937 (the default), uni32, vax, tt800 etc. Typing in an illegal name will return the list of current valid names. See the GSL manual for details.

set_xrandom initializes the random number generator with a supplied integer seed. If the seed is 0, the current UNIX time (see time(3) ) is used instead. It returns the seed used.

xrandom returns a uniformly distributed random number between a (inclusize) and b (exclusive).

grandom returns a gaussian distributed number with mean m and dispersion s.

frandom returns a a random number according to an arbitrary given probability function func(x), defined between x=a and x=b. This is implemented by defining a spline for the inverse normalized cumulative distribution function (F(x), with F(a)=0, F(b)=1), then pulling uniform random numbers between 0 and 1 and returning the corresponding random number between a and b.

Both grandom and frandom call xrandom, which in turn is defined in terms of either the portable random generator (to be installed through the include file options.h), or the unix random number generator random(3) .

xrand is a simple random number generator, with the same arguments as xrandom, but uses a much simpler algorithm, using the standard rand(3) . See also srand(3) when a seed is needed. It is not recommended to use xrand if xrandom is available.


The frandom function defines a spline, and the spline coefficients for inversion will be initialized every time a new function is given. If two series of random numbers are needed, each with their own probability function, it is hence not such a good idea to do it within the same for-loop!


With the testbed program (make xrandomtest CFLAGS=) the following first four random numbers have been extracted:
CFLAGS=      first 4 random numbers [0,1)
             0.968071 0.0667306 0.478281 0.909534
-DNUMREC     0.715119 0.0330211 0.874394 0.534194
-DRAND48     0.0416303 0.454492 0.834817 0.335986

See Also

random(3) , rand(3) , srand(3) , drand48(3)


~/src/kernel/misc    xrandom.c frandom.c xrand.c


Josh Barnes, Peter Teuben

Update History

xx-xxx-86    created in the dark ages                      JEB
13-sep-90    portable random number generated installed    PJT
17-sep-90    frandom written, for mass-spectra            PJT
15-apr-91    added doc for set_xrandom                   PJT
7-jan-92    fixed seed bug in set_xrandom (from 13-sep-90)    PJT
4-mar-94       documented xrand      PJT
24-feb-00    documented special -1,-2 seeds              PJT
8-sep-01    starting a GSL optional implementation, added init_xrandom    PJT

Table of Contents