Next: Other random number generators, Previous: Random number generator algorithms, Up: Random Number Generation
The standard Unix random number generators rand
, random
and rand48
are provided as part of GSL. Although these
generators are widely available individually often they aren't all
available on the same platform. This makes it difficult to write
portable code using them and so we have included the complete set of
Unix generators in GSL for convenience. Note that these generators
don't produce high-quality randomness and aren't suitable for work
requiring accurate statistics. However, if you won't be measuring
statistical quantities and just want to introduce some variation into
your program then these generators are quite acceptable.
This is the BSD
rand
generator. Its sequence isx_{n+1} = (a x_n + c) mod mwith a = 1103515245, c = 12345 and m = 2^31. The seed specifies the initial value, x_1. The period of this generator is 2^31, and it uses 1 word of storage per generator.
These generators implement the
random
family of functions, a set of linear feedback shift register generators originally used in BSD Unix. There are several versions ofrandom
in use today: the original BSD version (e.g. on SunOS4), a libc5 version (found on older GNU/Linux systems) and a glibc2 version. Each version uses a different seeding procedure, and thus produces different sequences.The original BSD routines accepted a variable length buffer for the generator state, with longer buffers providing higher-quality randomness. The
random
function implemented algorithms for buffer lengths of 8, 32, 64, 128 and 256 bytes, and the algorithm with the largest length that would fit into the user-supplied buffer was used. To support these algorithms additional generators are available with the following names,gsl_rng_random8_bsd gsl_rng_random32_bsd gsl_rng_random64_bsd gsl_rng_random128_bsd gsl_rng_random256_bsdwhere the numeric suffix indicates the buffer length. The original BSD
random
function used a 128-byte default buffer and sogsl_rng_random_bsd
has been made equivalent togsl_rng_random128_bsd
. Corresponding versions of thelibc5
andglibc2
generators are also available, with the namesgsl_rng_random8_libc5
,gsl_rng_random8_glibc2
, etc.
This is the Unix
rand48
generator. Its sequence isx_{n+1} = (a x_n + c) mod mdefined on 48-bit unsigned integers with a = 25214903917, c = 11 and m = 2^48. The seed specifies the upper 32 bits of the initial value, x_1, with the lower 16 bits set to
0x330E
. The functiongsl_rng_get
returns the upper 32 bits from each term of the sequence. This does not have a direct parallel in the originalrand48
functions, but forcing the result to typelong int
reproduces the output ofmrand48
. The functiongsl_rng_uniform
uses the full 48 bits of internal state to return the double precision number x_n/m, which is equivalent to the functiondrand48
. Note that some versions of the GNU C Library contained a bug inmrand48
function which caused it to produce different results (only the lower 16-bits of the return value were set).