Page 211 - Introduction to Programming with Java: A Problem Solving Approach
P. 211
5.8 Problem Solving with Random Numbers (Optional) 177
in more than one place, it’s a good idea to save the format string in a named constant and use the named con- stant in the printf statements. By storing the format string in one common place (in a named constant), you ensure consistency and you make it easier to update the format string in the future.
In the BudgetReport program, note the minus sign in the HEADING_FMT_STR and DATA_FMT_STR format strings. That left justifies the first column’s data. Note the commas in the DATA_FMT_STR format string. That causes locale-specific characters (commas in the United States) to appear between every third digit at the left of the decimal point. Note the left parenthesis in the DATA_FMT_STR format string. That causes negative numbers to use parentheses instead of a minus sign.
5.8 Problem Solving with Random Numbers (Optional)
This section will show you how to generate random variables that have probability distributions different
from the 0.0 to 1.0 uniform distribution assumed in a simple Math.random method call. Using Math.random to Generate Random Numbers with
Other Probability Distributions
As indicated in Figure 5.2, in Section 5.3, when you need a random number, you can use the Math.random
method to generate one. Suppose you want a random number from a range that’s different from the range 0.0
to 1.0. As we did in the initialization of winningNumber in Figure 5.5, you can expand the range to any
maximum value by multiplying the random number generated by Math.random() by your desired maxi-
mum value. You can also offset the range by adding or subtracting a constant. For example, suppose you
Apago PDF Enhancer
want to pick a random number that’s uniformly distributed in the range between 5.0 and 15.0. Instead of using just plain old Math.random(), use this:
(20.0 * Math.random()) - 5.0.
It’s possible to manipulate numbers produced by Math.random to get any kind of distribution you want. For example, you can generate any of the distributions shown in Figure 5.11.
Now, let’s look at how to generate these five types of random numbers from Math.random.
1. The first type (a continuous uniform distribution) is easy. To get a value for a random number, x, uni-
formly distributed in the interval between zero and unity (0.0 x 1.0), use a statement like this: double r1 = Math.random();
This first type of random number is the basis of all other types of random numbers.
2. For the second type (an offset and expanded continuous uniform distribution), you must have some minimum and maximum values, for example:
double minReal = 1.07; // meters for shortest adult human
double maxReal = 2.28; // meters for tallest adult human
Then you shift and expand the basic random number by using a statement like this:
double r2 = minReal + Math.random() * (maxReal - minReal);
3. For the third type (a discrete uniform distribution), you create integer versions of the limits, for example: