- Include the header file stdlib.h.
**Once**before you use rand you need to "seed" the random number generator using the*srand*function. There are two ways you can do this:- Ask the user for a seed (which is an unsigned integer), then
call srand with the that seed as a parameter. The code is as
follows:
unsigned seed; cout << "Enter a seed for the random number generator: "; cin >> seed; srand (seed);

- Seed the random number generator with the system time. For this method
you need to include the header file time.h. The code is
as follows:
srand(unsigned (time ( (time_t *) NULL) ) );

- Ask the user for a seed (which is an unsigned integer), then
call srand with the that seed as a parameter. The code is as
follows:
- Whenever you need a random number, call the function rand(),
which returns an integer between 0 and RAND_MAX (a big number!).
You can use modular arithmetic to get a number in whatever
range you want. Some
examples,
x = rand(); // an integer between 0 and RAND_MAX x = rand() % 1000; // an integer between 0 and 999 x = 5 + rand() % 10; // an integer between 5 and 14 y = rand()/ (double) rand(); // a real number

The C++ Library has other classes for random number generation. You are welcome to use any of them.