Standard C Library Functions rand(3C)
NAME
rand, srand, randr - simple random-number generator
SYNOPSIS
#include
int rand(void);
void srand(unsigned int seed);
int randr(unsigned int *seed);
DESCRIPTION
The rand() function uses a multiplicative congruential
random-number generator with period 2^32 that returns suc-
cessive pseudo-random numbers in the range of 0 to RANDMAX
(defined in ).
The srand() function uses the argument seed as a seed for a
new sequence of pseudo-random numbers to be returned by sub-
sequent calls to rand(). If srand() is then called with the
same seed value, the sequence of pseudo-random numbers will
be repeated. If 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.
The randr() function has the same functionality as rand()
except that a pointer to a seed seed must be supplied by
the caller. If randr() is called with the same initial
value for the object pointed to by seed and that object is
not modified between successive calls to randr(), the same
sequence as that produced by calls to rand() will be gen-
erated.
The rand() and srand() functions provide per-process
pseudo-random streams shared by all threads. The same effect
can be achieved if all threads call randr() with a pointer
to the same seed object. The randr() function allows a
thread to generate a private pseudo-random stream by having
the seed object be private to the thread.
USAGE
The spectral properties of rand() are limited. The
drand48(3C) function provides a better, more elaborate
random-number generator.
SunOS 5.11 Last change: 19 May 2004 1
Standard C Library Functions rand(3C)
When compiling multithreaded applications, the RENTRANT
flag must be defined on the compile line. This flag should
be used only in multithreaded applications.
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Standard
MT-Level Safe
SEE ALSO
drand48(3C), attributes(5), standards(5)
SunOS 5.11 Last change: 19 May 2004 2
|