Turbo C++ provides a function random:
int random(int max);
which returns an integer that is between 0 and max - 1. The implementation of the function looks something like this:
int random(int maxValue)
{
return rand() % maxValue;
}
Unfortunately, not all compilers have this function so you can't use it. However, it is easy to write your own that will work everywhere, and is more versatile:
int generateRandom(int lowValue, int highValue)
{
int number;
number = rand() % (highValue - lowValue + 1) +
lowValue;
return number;
}
Example calls:
int number;
number = generateRandom(0, 10); // 0 <= number <= 10
number = generateRandom(70, 100); // 70 <= number <= 100
number = generateRandom(-10, 10); // -10 <= number <= 10