rand(3C) rand(3C) NAME rand, srand, rand_r - simple random-number generator SYNOPSIS #include <stdlib.h> int rand (void); int rand_r (unsigned int *seed); void srand (unsigned int seed); DESCRIPTION rand uses a multiplicative congruent random-number generator with period 2^32 that returns successive pseudo-random numbers in the range from 0 to (2^15)-1. The function srand uses the argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to the function rand. If the function srand is then called with the same seed value, the sequence of pseudo-random numbers will be repeated. If the function rand is called before any calls to srand have been made, the same sequence will be generated as when srand is first called with a seed value of 1. rand_r is a reentrant version of rand. If rand_r is called with the same initial value for *seed and the value of *seed is not changed between successive returns and calls to rand_r, the same sequence shall be generated. This function is useful when multiple threads in a process each wish to get their own repeatable pseudo-random number sequence. The feature test macro _SGI_REENTRANT_FUNCTIONS should be defined to make this function visible. NOTES The spectral properties of rand are limited. drand48(3C) and random(3B) provide a much better, though more elaborate, random-number generator. In a multi-threaded program rand is made MP safe by single-threading access to the shared seed. For best performance threaded applications should use rand_r instead. SEE ALSO drand48(3C), random(3B) Page 1