Java Reference
In-Depth Information
If you use the default constructor, the sequence of numbers that is generated will be different each time
a program is run, although beware of creating two generators in the same program with the default
constructor. The time resolution used is one millisecond, so if you create two objects in successive
statements they will usually generate the same sequence because the times used for the starting seed
values will be identical. Random objects created using the same seed will always produce the same
sequence, which can be very important when you are testing a program. Testing a program where the
output is not repeatable can be a challenge! A major feature of random number generators created using
a given seed in Java is that not only will they always produce the same sequence of pseudo-random
numbers from a given seed, but they will also do so even on totally different computers.
Random Operations
The public methods provided by a Random object are:
Method
Description
nextInt()
Returns a pseudo-random number of type int . Values generated will be
uniformly distributed across the complete range of values for a number of
type int .
nextInt
(int limit)
Returns a pseudo-random number of type int that is greater than or equal to
0, and less than limit - very useful for creating random array index values.
nextLong()
Returns a pseudo-random number of type long . Values generated will be
uniformly distributed across the complete range of values for a number of
type long .
nextFloat()
Returns a pseudo-random number of type float . Values generated will be
uniformly distributed across the range 0.0f to 1.0, including 0.0f but
excluding 1.0f.
nextDouble()
Returns a pseudo-random number of type double . Values generated will be
uniformly distributed across the range 0.0 to 1.0, including 0.0 but excluding 1.0.
nextGaussian()
Returns a pseudo-random number of type double selected from a Gaussian
distribution. Values generated will have a mean of 0.0 and a standard
deviation of 1.0.
nextBoolean()
Returns true or false as pseudo-random values.
nextBytes
(byte[] bytes)
Fills the array, bytes , with pseudo-random values.
setSeed
(long seed)
Resets the random number generator to generate values using the value
passed as an argument as a starting seed for the algorithm.
To produce a pseudo-random number of a particular type, you just call the appropriate method for a
Random object. You can repeat the sequence of numbers generated by a Random object that you
created with a seed value, by calling the setSeed() method with the same seed value as an argument.
Search WWH ::




Custom Search