Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 17 ■ i3D Game Square SeleCtion: uSinG the piCkreSult ClaSS with 3D moDelS


Finally, the .ints(long streamSize, int randomNumberOrigin, int randomNumberBound) method
call will return a stream of numeric (integer) values that produces the streamSize number of pseudorandom
int values that are specified in the parameter area, with each value conforming to the specified binding
origin (inclusive) and bound limit (exclusive), which are also taken from the method call parameter area.
The .longs() method call will return an unlimited stream of pseudorandom long numeric values,
called a LongStream. There are three additional overloaded .longs() method calls, including a .longs(long
randomNumberOrigin, int randomNumberBound) method call, which will return an unlimited stream
of pseudorandom long values, each of which will conform to a binding origin (inclusive) and bound limit
(exclusive) value specified in the parameter area. The .longs(long streamSize) method call will return a
random long values stream that produces the stream size that is specified using the streamSize parameter
that establishes a desired number of pseudorandom long values.
Finally, a .long(long streamSize, int randomNumberOrigin, long randomNumberBound) method
call will return a stream of numeric long values that produces the streamSize number of pseudorandom long
values that are specified in the parameter area, with each value conforming to the specified binding origin
(inclusive) and bound limit (exclusive), which are also taken from the method call parameter area.
The protected int .next(int bits) method call will generate the next pseudorandom integer number
using an integer number of bits as the parameter specification. The .nextBoolean() method call will
return a pseudorandom, uniformly distributed, boolean value from the random number generator object’s
sequence. This method probably shouldn’t be used for this game’s use case because next() is designed to be
called by other random() methods.
The void .nextBytes(byte[] bytes) method call will generate a parameter-supplied byte array and
fill it with random byte values. The .nextDouble() method call will return a pseudorandom, uniformly
distributed, double value between the values of 0.0 and 1.0 by using a random number generator object’s
sequence. The .nextFloat() method call will return a pseudorandom, uniformly distributed, float (or
floating-point) value, between 0.0 and 1.0, using the random number generator object’s sequence.
The .nextGaussian() method call will return a pseudorandom, Gaussian distribution, double value,
with its mean at 0.0 and its standard deviation at 1.0, from this random number generator object’s sequence.
The .nextInt() method call will return the next pseudorandom, uniformly distributed, int (integer) value
from this random number generator object’s sequence.
The .nextLong() method call will return the next pseudorandom, uniformly distributed, long value
from this random number generator object’s sequence.
The void .setSeed(long seed) method call can be used to set (or reseed) the seed of the random
number generator object using a single long value seed specification inside of the parameter area for the
method call.
Finally, the .nextInt(int bound) method call, which is the one that we are going to utilize in the final
section of this chapter, will return a pseudorandom, uniformly distributed, int (integer) value between 0
(inclusive) and the specified value (exclusive), in our case 4, drawn from the random number generator
object’s random int sequence.


Random Quadrant Selection: Using Random with Conditional If()


Now that we have set up our spinner and game board rotation and MouseEvent handling well enough
to connect the two together to create a random spin for the game board, we need to add a randomizer
algorithm to the code so that each time a spinner is clicked, the game board is randomly set to a new
quadrant. We’ll use at least three rotations so that the spin is long enough to appear completely random to
the player. Let’s declare a Random object named random at the top of the class and then use the Alt+Enter
keystroke combination to bring up the NetBeans 9 pop-up helper. Finally, select (double-click) the “Add
import for java.util.Random” option, as shown in blue in Figure 17-11.

Free download pdf