|
|
#include <stdlib.h>int rand (void); void srand(unsigned int seed);
srand- reset random-number generator
The rand function 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 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.
The following functions define the semantics of the functions 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 different implementations.
X/Open Portability Guide, Issue 3, 1989
;
ANSI X3.159-1989 Programming Language -- C
;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1)
;
and
NIST FIPS 151-1
.