Java Reference
In-Depth Information
The first time it is invoked, it initializes the seed with a value derived from the
current time.
The
java.util.Random
class provides a more extensive set of random
generators. Two constructors -
Random()
and
Random (long seed)
-offer
the options of initialization from the current time or from a specific
seed
value.
The methods in the
Random
class include:
nextInt ()
- integers in the range
0 <= r < 2**32
nextInt (int n)
- integers in the range
0 <= r < n
nextBoolean (int n)
- randomly chosen true/false
nextGaussian ()
- random double values with mean 0.0 and sigma of 1.0
The last three methods first became available with Java 1.2.
4.9.1 Random number algorithm
The
Random
class uses a
linear congruential algorithm
[1,2] with a 48-bit seed. If
the constructor
Random (long)
or the
setSeed (long)
method is invoked,
the algorithm uses only the lower 48 bits of the seed value.
Random number generator formulas actually produce a sequence of numbers
that eventually repeat. For the same seed value a formula always produces the same
sequence. A seed simply selects where in the sequence to start. The generator will
eventually repeat that seed value and start the same sequence again. Compared
to the randomness of physical fluctuations, such as in radio noise, these formulas
are said to produce
pseudo-random
numbers.
To insure that applications ported to different platforms give the same results,
all implementations of Java must use the same algorithm so that the same seed
returns the same sequence regardless of the platform.
The linear congruential formula in Java goes as
=
(a*x
i
+c)mod m
x
i
+
1
As discussed in the references, you should use such formulas with care. They can
produce random number sequences of a length up to
m
but not necessarily that
long. The length depends on the set of
a
,
c
, and
m
values chosen.
Also, if you grab consecutive sequences of numbers of
K
length, and plot them
as points in
K
-dimensional space, they do not fully populate the volume randomly
but instead lie on
K-1
dimensional planes. There are no more than
m
1/K
planes
and possibly less. If you need to create points in a space this way, you should
shuffle the values obtained from the generator. [2]
In Java the values in the linear congruential formula in
Random
are
a = 0x5DEECE66DL
c = 11
m = 2
48
−
1.
The actual code in
next (int bits)
goes as
Search WWH ::
Custom Search