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

Using java.util.Random: Generating a Random Spin


The public class Random extends Object and implements Serializable. It is kept in the java.util package
and has two known direct subclasses, SecureRandom and ThreadLocalRandom. An instance of this
class can be used to create a random number-generating object, which will generate a stream of
“pseudorandom numbers.” These numbers will be random enough for the purposes of creating random
game spinner UI functionality. The algorithm for this class uses a 48-bit seed, which is modified using a
linear congruential formula. If you want to research this algorithm in more detail, you can reference The
Art of Computer Programming (Volume 2, Section 3.2.1) by Donald Knuth. The Java class hierarchy for the
Random class would therefore look like the following:


java.lang.Object



java.util.Random



It’s important to note that if two different instances of Random objects are created using the same seed
and the same sequence of method calls is made for each object, an algorithm generates (returns) the same
sequence of numeric results. In some applications, this is actually desirable; so, to guarantee an identical
result, specific algorithms are implemented for the java.util.Random class. Subclasses of class Random are
permitted to use alternate algorithms for increased security or multithread use, as long as they adhere to
general contracts for all the methods.
The instances of java.util.Random are thread-safe. However, a concurrent use of the same java.util.Random
instance across multiple threads could encounter contention and consequently result in poor performance. You
should consider using the ThreadLocalRandom subclass for your multithreaded game designs.
Additionally, instances of java.util.Random are not cryptographically secure. You should consider
instead using the SecureRandom subclass to get a cryptographically secure, pseudorandom number
generator for use by applications that are sensitive and that require a high level of security.
There are two overloaded constructor methods for this class. The first creates a random number
generator, and the second creates a random number generator and gives it a seed value using a long format.
These constructor methods look like the following Java code:


Random() // We'll be using this in our code later on during this chapter
Random(long seed)


This class has 22 methods that can be used to obtain random number results from the Random object.
The .doubles() method call will return an unlimited stream of numeric values called a DoubleStream,
which contains pseudorandom double values. Each of these values will fall between zero (inclusive) and
one (exclusive). There are three additional overloaded .doubles() method calls. The .doubles(double
randomNumberOrigin, double randomNumberBound) method call will return an unlimited bound
stream of pseudorandom double values, each conforming to the given binding origin (inclusive) and bound
limit (exclusive) specified in the method call parameter area. The .doubles(long streamSize) method
call will return a stream that produces the given streamSize number of pseudorandom double values that
are between zero (inclusive) and one (exclusive). Finally, there is a .doubles(long streamSize, double
randomNumberOrigin, double randomNumberBound) method call that returns a stream that produces
a stream conforming to the given streamSize number of pseudorandom double values, each conforming to
the given binding origin (inclusive) and bound limit (exclusive).
The .ints() method call will return an unlimited stream of pseudorandom int (integer) numeric values
called an IntStream. There are three additional overloaded .ints() method calls, including an .ints(int
randomNumberOrigin, int randomNumberBound) method call, which will return an unlimited stream
of pseudorandom int (integer) values, each of which will conform to a binding origin (inclusive) and bound
limit (exclusive) value specified in the parameter area. The .ints(long streamSize) method call will return
a random values stream, which produces a stream size that is specified using the streamSize parameter that
establishes a desired number of pseudorandom int (integer) values.

Free download pdf