As with most aspects of C++, there is a function in the standard C++ library to help seed random numbers. The function is srand, and it's declaration looks like this:
void srand(int seed);
The parameter seed is used to "shuffle" the list of random numbers by starting somewhere in the middle of the list. Now, the following program prints a different set each time it is run:
#include <iostream.h> // for cout
#include <stdlib.h> // for rand()
#include <time.h> // for time(), time_t
void shuffleRandomNumbers(void);
void main(void)
{
shuffleRandomNumbers();
for (int x = 0; x < 5; x++)
cout << setw(5) << rand() % 10 + 1;
}
void shuffleRandomNumbers(void)
{
time_t seconds;
seconds = time(NULL); // seconds since 1/1/70
srand( int(seconds) ); // seed random numbers
}
1st run: 9 6 5 3 1
2nd run: 4 3 6 2 8
3rd run: 3 6 4 4 8