// Understanding rand() and srand() // Pseudorandom numbers and seeds // Written by Lee Devlin 04-06-2011 // the rand() function generates a pseudorandom sequence, if you want to make it change // each time the program is run, you need to seed it with a number that keeps changing // The time(0) function is most often used for this purpose. // This program illustrates the issue of running srand() continuously, which can produce // very undesirable results. #include using namespace std; int main() { for(int i=0; i<10; i++) { srand(time(0)); // This will not have the desired effect because time changes once per second // and this loop executes much faster than that. int r = rand(); cout << "random number "<< i <<" is " << r << " time is " << time(0) << endl; } system("pause"); }