1: // Filename: RAND.CPP 2: // Purpose: Illustrates the system random number generator. 4: #include <iostream> 5: #include <cstdlib> 6: #include <ctime> 7: using namespace std; 9: #include "PAUSE.H" 11: int main() 12: { 13: cout << "\nThis program illustrates three system " 14: << "\"utility\" functions that are available " << endl 15: << "from the standard libaries: clock(), " 16: << "rand() and srand() " << endl << endl; 17: Pause(0); 19: cout << "\nThe clock() function from the <ctime> " 20: << "library returns the number of CPU " << endl 21: << "\"clock ticks\" since your process " 22: << "started running. " << endl; 23: cout << "Current value of clock(): " << clock() << endl; 24: Pause(0); 26: cout << "\nRAND_MAX is a named constant from <cstdlib>." << endl; 27: cout << "It is the largest possible return value " 28: << "from a call to the rand() function. " << endl; 29: cout << "Value of RAND_MAX on this system: " << RAND_MAX << endl; 30: Pause(0); 32: // Run this program three times. Then un-comment the function call given 33: // below and run it three more times. Be sure you understand that (even 34: // if not how) the function "resets" the system "random number generator". 35: // srand(clock()); 37: int i; 38: cout << "\nHere are 10 \"random\" integer values " 39: << "in the range 0.." << RAND_MAX << ": " << endl; 40: for (i = 1; i <= 10; i++) 41: cout << rand() << endl; 42: Pause(0); 44: int first, second; 45: cout << "\nEnter two positive integers, with " 46: << "the second larger than the first: "; 47: cin >> first >> second; cin.ignore(80, '\n'); cout << endl; 48: cout << "Here are 10 random integer values between " 49: << first << " and " << second << ": " << endl; 50: for (i = 1; i <= 10; i++) 51: cout << first + rand() % (second - first + 1) << endl; 52: cout << endl; 54: return 0; 55: }