RAND(3) BSD Programmer's Manual RAND(3)NAME
rand, rand_r, srand - bad random number generator
SYNOPSIS
#include <stdlib.h>
void
srand(unsigned seed);
int
rand(void);
int
rand_r(unsigned int *seed);
DESCRIPTION
These interfaces are obsoleted by random(3).
The rand() function computes a sequence of pseudo-random integers in the
range of 0 to RAND_MAX (as defined by the header file <stdlib.h>).
The srand() function sets its argument as the seed for a new sequence of
pseudo-random numbers to be returned by rand(). These sequences are re-
peatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically seeded
with a value of 1.
The rand_r() function computes a sequence of pseudo-random integers as in
rand() but the value pointed to by seed is used to seed the random-number
calculation.
Calling rand(),and srand() within a multi-threaded application is sup-
ported and provides a single process-specific random number sequence that
is shared by the calling threads. The rand_r() function may be used to
provide a per-thread random number sequence.
The generator is a linear congruential type as described in Knuth, The
Art of Computer Programming: Vol. 2, Seminumerical Algorithms, 1981.
Each successive term is determined by the formula:
x = x * 1103515245 + 12345
The modulus is achieved by using C's rules for unsigned arithmetic, and
is therefore a power of two. By the mathematics governing such genera-
tors, reducing this generator by a multiple of any smaller power of two
also reduces the corresponding sequence period. For instance, the low
bit of each successive term is alternately 0 and 1, regardless of the
seed. In this sense, the upper bits of each number are ``more random''
than the lower bits. Thus, although the RAND_MAX constant is 32768, the
rand() and rand_r() functions discard the lowest 16 bits of each term and
return the next 15 bits. Users should discard additional low-order bits
from the return value first, or better yet, use one of the alternative
generators such as lrand48(3) or random(3).
SEE ALSOlrand48(3), random(3)STANDARDS
The rand() and srand() functions conform to ANSI C X3.159-1989 (``ANSI C
''). The rand_r() function conforms to IEEE Std1003.1c (``POSIX'').
BSDI BSD/OS June 4, 1993 1