#include <stdlib.h>
int
rand
(void);
The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.
The implementation shall behave as if no library function calls the rand function.
The rand function returns a pseudo random integer.
The value of RAND_MAX is 0x7fff.
#include <stdlib.h>
void
srand
(unsigned int
seed );
The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.
The implementation shall behave as if no library function calls the srand function.
The srand function returns no value.
The following functions define a portable implementation of rand and srand.
unsigned long next = 1; int rand (void) { next = next * 1103515245L + 12345L; return (unsigned int)((next > 16) & 0x7fff; } void srand (unsigned int seed) { next = seed; }
GCC-1750 uses the algorithm in the example above, and the value of RAND_MAX is 32767.