//--------------------------------------------------------- // Demonstrate rand() and srand(). //--------------------------------------------------------- #include <iostream> #include <iomanip> using namespace std; int main() { char ch; cout << "RAND_MAX = " << RAND_MAX << endl << endl; // Display a sequence of 13 random values for(int i = 0; i < 13; i++) { cout << setw(6) << rand(); } cout << endl << endl; // Using the same seed value, display 3 sequences // of 13 random values. for(int i = 0; i < 3; i++) { srand(0); for(int j = 0; j < 13; j++) { cout << setw(6) << rand(); } cout << endl; } cout << endl; // Using the different seed values, display 3 sequences // of 13 random values. for(int i = 0; i < 3; i++) { srand(i); for(int j = 0; j < 13; j++) { cout << setw(6) << rand(); } cout << endl; } cout << endl; // "Randomize" the number generator for(int i = 0; i < 3; i++) { srand(time(NULL)); for(int j = 0; j < 13; j++) { cout << setw(6) << rand(); } cout << endl; cout << "Tap 'Enter' to continue..."; cin.get(ch); cout << endl; } cout << endl; // Display values between 0.0 and 1.0 (not including 1.0). cout << fixed << setprecision(3); for(int i = 0; i < 13; i++) { cout << setw(6) << double(rand())/double(RAND_MAX + 1); } cout << endl << endl; return 0; }