/* rand.cpp * * Test generation/limits of random numbers. * Writes information to standard output (cout) * and two different files (test3.out and test4.out). * * Bruce M. Bolden * October 9, 1997 * ------------------------------------------------- */ #include #include #include #include #include // prototypes void Test1( int n ); void Test2( int n, int rMax ); void Test3( ofstream& fOut, int n ); void Test4( int n, double rMin, double rMax ); main() { cout << "Random Number Testing\n\n" << endl; // What is RAND_MAX? 32,767 using g++ cout << "RAND_MAX: " << RAND_MAX << endl; // Random number generator can be seeded with current time // so that the numbers will be different every time the // program is run. // Be careful: This can make testing difficult. srand( (unsigned)time( NULL ) ); cout << "\nRandom numbers (full range)\n" << endl; Test1( 10 ); // Generate 10 random numbers cout << "\nRandom numbers (0..100)\n" << endl; Test2( 10, 100 ); ofstream fOut( "test3.out", ios::out ); fOut << "\nRandom number distribution (0..5)\n" << endl; if( !fOut ) { cerr << "Unable to open \"test3.out\"" << endl; } else { for( int i = 0 ; i < 20 ; ++i ) { Test3( fOut, 600 ); } fOut.close(); } cout << "\nRandom numbers (5.0..10.0)\n" << endl; Test4( 50, 5.0, 10.0 ); } /* Test1 * Generate n random numbers. */ void Test1( int n ) { for( int i = 0 ; i < n ; i++ ) { //printf( " %6d\n", rand() ); cout << " " << setw(6) << rand() << endl; } } /* Test2 * Generate n random numbers within the specified * range: 0..rMax */ void Test2( int n, int rMax ) { rMax++; for( int i = 0 ; i < n ; i++ ) { cout << " " << setw(6) << (rand() % rMax) << endl; } } /* Test3 * Generate n random numbers within the specified * range: 0..5 and tabulate them. */ void Test3( ofstream& fOut, int n ) { // Save the test number between usages by // declaring testNumber to be static. static int testNumber = 1; int n0 = 0; // counters---array would be better! int n1 = 0; int n2 = 0; int n3 = 0; int n4 = 0; int n5 = 0; for( int i = 0 ; i < n ; i++ ) { int iRand = (rand() % 6); switch( iRand ) { case 0: n0++; break; case 1: n1++; break; case 2: n2++; break; case 3: n3++; break; case 4: n4++; break; case 5: n5++; break; default: cerr << "Error generating random number!" << endl; } } // show results fOut << "\nTest #" << testNumber++ << endl; fOut << " 0: " << setw(5) << n0 << endl; fOut << " 1: " << setw(5) << n1 << endl; fOut << " 2: " << setw(5) << n2 << endl; fOut << " 3: " << setw(5) << n3 << endl; fOut << " 4: " << setw(5) << n4 << endl; fOut << " 5: " << setw(5) << n5 << endl; } /* Test4 * Generate n random numbers within the specified * range: rMin..rMax * Note: rMin and rMax are real numbers. */ void Test4( int n, double rMin, double rMax ) { double rVal; // random value double range = rMax - rMin + 1.0; ofstream fOut( "test4.out", ios::out ); for( int i = 0 ; i < n ; i++ ) { // move range calculation outside loop! //rVal = rMin + double(rand() % int(rMax-rMin+1.0)); rVal = rMin + double(rand() % int(range)); //cout << " " << setw(6) << rVal << endl; fOut << " " << setw(6) << rVal << endl; } fOut.close(); }