SOURCE FILE: 6.12RandDoubles.cpp
// Example 6.12. Test program for random_double and random_double_range
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
int main(void)
{
double random_double(void);
double random_double_range(double LowerBound, double UpperBound);
int i = 0;
cout << "Random doubling point numbers in range\n";
cout << setw(20) << "0-1" << setw(20) << "3-7" << "\n\n";
while (i < 10)
{
cout << setw(20) << random_double()
<< setw(20) << random_double_range(3, 7) << endl;
i++;
}
return 0;
}
//
// Function to return a random double between 0 and 1
// Pre: The generator is seeded.
// Post: A random double greater than or equal to 0
// and less than 1 has been returned.
//
double random_double(void)
{
return ( rand() / (double(RAND_MAX) + 1) );
}
//
// Function to generate a random doubling point number
// between LowerBound and UpperBound
// Pre: LowerBound and UpperBound are of type double with
// LowerBound < UpperBound. The generator is seeded.
// Post: A random double greater than or equal to LowerBound
// and less than UpperBound has been returned.
//
double random_double_range(double LowerBound, double UpperBound)
{
double random_0_1; // random number between 0 and 1
random_0_1 = rand() / (double(RAND_MAX) + 1);
return ((UpperBound - LowerBound) * random_0_1
+ LowerBound);
}