26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Methods 261<br />

6.8 Random-Number Generation<br />

We now take a brief and, hopefully, entertaining diversion in<strong>to</strong> a popular programming application,<br />

namely, simulation and game playing. In this section and the next section, we will<br />

develop a nicely structured game-playing program that includes multiple methods. The<br />

program uses most of the control structures we have studied <strong>to</strong> this point in the book and<br />

introduces several new concepts.<br />

There is something in the air of a gambling casino that invigorates people—from the<br />

high rollers at the plush mahogany-and-felt craps tables <strong>to</strong> the quarter poppers at the onearmed<br />

bandits. It is the element of chance, the possibility that luck will convert a pocketful<br />

of money in<strong>to</strong> a mountain of wealth. The element of chance can be introduced through the<br />

random method from the Math class. [Note: <strong>Java</strong> also provides a Random class in<br />

package java.util. Class Random is covered in Chapter 20.]<br />

Consider the following statement:<br />

double randomValue = Math.random();<br />

The random method of class Math generates a random double value from 0.0 up <strong>to</strong>, but<br />

not including, 1.0. If method random truly produces values at random, then every value<br />

from 0.0 up <strong>to</strong>, but not including, 1.0 should have an equal chance (or probability) of being<br />

chosen each time method random is called. Note that the values returned by random are<br />

actually pseudo-random numbers—a sequence of values produced by a complex mathematical<br />

calculation. That calculation uses the current time of day <strong>to</strong> seed the random number<br />

genera<strong>to</strong>r, such that each execution of a program yields a different sequence of random<br />

values.<br />

The range of values produced directly by method random often is different from the<br />

range of values required in a particular <strong>Java</strong> application. For example, a program that simulates<br />

coin <strong>to</strong>ssing might require only 0 for “heads” and 1 for “tails.” A program that simulates<br />

rolling a six-sided die would require random integers in the range from 1 <strong>to</strong> 6. A<br />

program that randomly predicts the next type of spaceship (out of four possibilities) that<br />

will fly across the horizon in a video game would require random integers in the range from<br />

1 <strong>to</strong> 4.<br />

To demonstrate method random, let us develop a program that simulates 20 rolls of<br />

a six-sided die and displays the value of each roll. We use the multiplication opera<strong>to</strong>r (*)<br />

in conjunction with method random as follows <strong>to</strong> produce integers in the range from 0<br />

<strong>to</strong> 5:<br />

(int) ( Math.random() * 6 )<br />

This manipulation is called scaling the range of values produced by Math method random.<br />

The number 6 in the preceding expression is called the scaling fac<strong>to</strong>r. The integer cast<br />

opera<strong>to</strong>r truncates the floating-point part (the part after the decimal point) of each value<br />

produced by the expression. Next, we shift the range of numbers produced by adding 1 <strong>to</strong><br />

our previous result, as in<br />

1 + (int) ( Math.random() * 6 )<br />

Figure 6.7 confirms that the results of the preceding calculation are integers in the range<br />

from 1 <strong>to</strong> 6.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!