// Dave Goldsmith // Redwood High School // C++ Computer Programming // September 20, 1999 // Ramdom1.cpp /* This program demonstrates how to generate "pseudo" random numbers. It generates and neatly displays 50 pseudo random integers between 1 and 100 inclusive. Pseudo random numbers generated by the computer are always integers between 0 and a very large number (MAXINT). The remainder of that chosen number divided by a variable 'n' will always generate a "random" number between 0 and 'n-1'. Add 1 to the result to get an integer between 1 and 'n' inclusive. The generated numbers are actually pseudo (fake) random numbers because they are not truly random, but are based on a formula which must be "seeded." The formula (which is built into the language and cannot easily be modified) is seeded by plugging a number into it. Each different number used in the formula allows a different set of "random" numbers to be generated. Therefore, the best way to seed the generator is to base the seed on the computer's internal clock, since the clock is always producing numbers that are constantly changing. */ #include // Required for 'cin' and 'cout' #include // Required for 'setw()' #include // Required for 'srand()' and 'rand()' #include // Required for 'time()' int main() { int count; srand(time(0)); // Seed the random number generator for (count=1; count<=50; count++) { cout << setw(5) << rand() % 100 + 1; if (count % 10 == 0) // Display 10 numbers per row cout << endl; } cout << endl << endl; return 0; }