RAND(3C) INTERACTIVE UNIX System RAND(3C)
NAME
rand, srand - simple random-number generator
SYNOPSIS
int rand ( )
void srand (seed)
unsigned 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 from 0 to
(2^15)-1.
The srand function can be called at any time to reset the
random-number generator to a random starting point. The
generator is initially seeded with a value of 1.
SEE ALSO
drand48(3C).
NOTES
The spectral properties of rand are limited. The
drand48(3C) function provides a much better, though more
elaborate, random-number generator.
The following functions define the semantics of the func-
tions rand and srand.
static unsigned long int next = 1;
int rand()
{
next = next * 1103515245 + 12345;
return ((unsigned int) (next/65536) % 32768);
}
void srand(seed)
unsigned int seed;
{
next = seed;
}
Specifying the semantics makes it possible to reproduce the
behavior of programs that use pseudo-random sequences. This
facilitates the testing of portable applications in dif-
ferent implementations.
Rev. C Software Development Set Page 1