[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    rand(3C) manual page Table of Contents

Name

rand, srand, rand_r - simple random-number generator

Synopsis

#include <stdlib.h>

int rand(void);

void srand(unsigned int seed);

int rand_r(unsigned int *seed);

MT-Level

See the NOTES section of this page.

Description

rand() uses a multiplicative congruential 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() has the same functionality as rand() except that a pointer to a seed seed must be supplied by the caller. The seed to be supplied is not the same seed as in srand().

See Also

drand48(3C)

Notes

The rand_r() interface is as proposed in the POSIX.4a Draft #6 document, and is subject to change to be compliant to the standard when it is accepted.

When compiling multi-thread applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multi-thread applications.

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

rand() is unsafe in multi-thread applications. rand_r() is MT-Safe, and should be used instead. srand() is unsafe in multi-thread applications.


Table of Contents