// // Filename: pgm3_3b.cpp // // Description: Demonstration of random number generation. // // ENSC 104: Digital Computer Programming // // Instructor: Dr. Walsh // // Section: 2 // // Date Created: 03/03/98 // Last Modified: 03/03/98 // // Name: N/A // #include #include #include #include #include const int MAX_NUM = 6; const int SPACING = 5; const int NUM_OBS = 20; const int NUM_PER_LINE = 10; int main( int argc, char *argv[] ) { int i; int randon_num; // // Will ALWAYS generate the same 'random' numbers. // cout << "Without 'seed'!\n"; for ( i=1; i<=NUM_OBS; i++) { random_num = 1 + rand() % MAX_NUM; cout << setw(SPACING) << random_num; if ( !(i%NUM_PER_LINE) ) { cout << endl; } } // Will NOT always generate the same 'random' numbers. // You want to use the 'srand() to get a different // sequence of random numbers each time. srand( time( NULL) ); cout << "\nWith 'seed'!\n"; for ( i=1; i<=NUM_OBS; i++) { random_num = 1 + rand() % MAX_NUM; cout << setw(SPACING) << random_num; if ( !(i%NUM_PER_LINE) ) { cout << endl; } } return 0; }