Random Numbers

Some considerations for generating random numbers in C
programs.

Random-number generation is a complex topic. The book
Numerical Recipes in C, by William H. Press, Brian P. Flannery,
Saul A. Teukolsky and William T. Vetterling, covers practical
random-number generation issues in Chapter 7. The Art of
Computer Programming, by Donald E. Knuth has even more in
Volume 2, chapter 3.

Random-number generation in C usually uses the library functions
rand() and srand()

#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);

The description of rand and srand is the same for most C
compilers. The rand() function returns a pseudo-random integer
between 0 and RAND_MAX. The srand() function sets its argument
as the seed for a new sequence of pseudo-random integers to be
returned by rand(). These sequences are repeatable by calling
srand() with the same seed value. If no seed value is provided,
the rand() function is automatically seeded with a value
of 1. The srand() returns no value.