- Computers can't generate truly random numbers; they generate pseudo-random numbers
- Random numbers are used extensively to test programs; easier than typing input from the keyboard all day
- Used in many simulations (games) to add a sense of "chance" to the program
- Computers have built-in algorithms that produce pseudo-random numbers for programs to use
- One method of accessing these numbers is to call the C++ library function rand() which will return a random integer between 0 and 32767
#include <iostream.h> // for cout
#include <stdlib.h> // for rand()
void main(void)
{
for (int x = 0; x < 5; x++)
cout << rand() << endl;
}
Output:
346
130
10982
1090
17595