DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

rand(3C)


rand, rand_r, srand -- simple random-number generator

Synopsis

   #include <stdlib.h>
   

int rand(void);

void srand(unsigned int seed);

int rand_r(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 RAND_MAX (defined in stdlib.h).

rand_r computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. If rand_r is called with the same initial value for seed and the value of seed is unchanged between successive returns and calls to rand_r, then the same sequence of random numbers are generated.

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.

References

drand48(3C)

Notices

The spectral properties of rand and rand_r are limited. drand48(3C) provides a much better, though more elaborate, random-number generator.

Each thread that accesses one of the functions drand48, lrand48, mrand48, srand48, seed48, or lcong48 should be coded as per the following example:

   mutex_lock(I_am_using_drand48);
   value = FUNCTION();
   mutex_unlock(I_am_using_drand48);

where FUNCTION is one of those listed. The same mutex must be used for all six functions.


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004