Page 214 - Introduction to Programming with Java: A Problem Solving Approach
P. 214
180 Chapter 5 Using Pre-Built Methods
The nextDouble method does essentially the same thing as Math.random does. This distribution appears in the top graph in Figure 5.11. The zero-parameter nextInt method generates random integers uniformly from the entire range of integers, that is, from 2147483648 to 2147483647, inclusive. The one- parameter nextInt method generates random integers uniformly from zero to one less than the parameter value. This distribution is almost like what appears in Figure 5.11’s third graph for the special case of n 7, except zero is allowed also. The nextBoolean method generates random values of true or false. The nextGaussian method generates a double value from a distribution having a mean value of 0.0 and a standard deviation of 1.0.
Notice that the Random class methods do not have the static modifier, so they are not class meth- ods, and you cannot use the Random class name to access these methods. You must create an object first, and then use that object’s name to access these methods. To create an object from any class other than the String class, you need to call a constructor. A constructor is a special type of method that’s in charge of creating and initializing objects. To call a constructor, specify the Java keyword new, the name of the con- structor, and then an argument list surrounded by parentheses. For example, see the Random constructor call in Figure 5.12. Note that no arguments are passed to the constructor, so the constructor call’s parentheses are empty. Also note how the Random constructor call is assigned to a reference variable named random. The random object then generates two random numbers by calling the nextInt and nextGausssian methods. Due to the Integer.MAX _VALUE argument, nextInt generates a random number between 0 and one less than the maximum integer value. The nextGaussian method generates a random number drawn from a Gaussian distribution having a mean of 5.0 and a standard deviation of 0.8.
Apago PDF Enhancer
Using a Fixed Seed to Freeze a Random Number Sequence
You can call the Random constructor with no arguments, as shown in Figure 5.12, and you can also call it with one argument, where the argument is a seed. A seed provides a starting point for the internal state of the random number generator. Suppose you change the body of the main method in Figure 5.12 to this:
Random random = new Random(123);
System.out.println(5.0 + 0.8 * random.nextGaussian());
System.out.println(5.0 + 0.8 * random.nextGaussian());
System.out.println(5.0 + 0.8 * random.nextGaussian());
Now, if you run the program you’ll get this: Sample session:
3.8495605526872745
5.507356060142144
5.1808496102657315
If you run the program again, and again, and again, you’ll get exactly the same three “random” num- bers every time! The 123 seed establishes a starting point, and this determines the “random” sequence pre- cisely. If you pick a different seed, you’ll get a different sequence, but that sequence will always be the same as long as you stick with that particular seed. Now you know why the methods in the Random class are not class methods. They need an object to call the methods because the methods need to know some informa- tion the object contains—the seed and the current position in the random-number sequence.