Java Reference
In-Depth Information
simply returns
next(32)
, while
nextBoolean
returns
TRue
if
next(1)
is not
zero, else it returns
false
.
You can safely use
Random
from multiple threads.
The
Random
class specifies the algorithms to be used to generate the
pseudo-random numbers but permits different algorithms to be used
provided the general contract of each method is adhered to. The basic
algorithm (a linear congruential generator) is defined in the
next
method
and is used for all other methods except
nextGaussian
. You can create
your own random number generator by overriding the
next
method to
provide a different generating algorithm.
Exercise 22.5
: Given a certain number of six-sided dice, you can calcu-
late the theoretical probability of each possible total. For example, with
two six-sided dice, the probability of a total of seven is one in six. Write
a program that compares the theoretical distribution of sums for a given
number of six-sided dice with the actual results over a large number of
"rolls" using
Random
to generate numbers between one and six. Does it
matter which of the number-generating methods you use?
Exercise 22.6
: Write a program that tests
nextGaussian
, displaying the
results of a large number of runs as a graph (a bar chart of
*
characters
will do).