// // Example 6.13. Program to print random integers in the range // 0 - (upperBound - 1) with seed based on time // #include #include #include #include using namespace std; int main() { void seedRand(); int randomInt(int upperBound); const int HIGH = 100; int i = 0; seedRand(); cout << "Random integers between 0 and " << HIGH - 1 << ", inclusively:" << endl; while (i < 10) { cout << setw(5) << randomInt(HIGH); ++i; } return 0; } // // Function to seed the random number generator // Pre: none // Post: The random number generator has been seeded. // void seedRand() { srand( static_cast(time((time_t *)NULL)) ); } // // Function to generate a random integer between // 0 and upperBound - 1 // Pre: upperBound is a positive integer. The generator is seeded. // Post: A random integer between 0 and upperBound - 1 // has been returned. // int randomInt(int upperBound) { double random_0_1; // random number between 0 and 1 random_0_1 = rand() / (static_cast(RAND_MAX) + 1); return static_cast(upperBound * random_0_1); }