Methods | Use | Notes |
nextInt(...) |
Generate a random value between 0 (inclusive) and the given upper bound (exclusive). This is probably
the most common method that most developers will need.
|
To generate a random number between x and y, this means that you do x + r.nextInt(y).
|
nextInt() nextLong() |
Generate 32 or 64 "raw" random bits. |
This is essentially the "rawest" call and will generate a random number across the entire possible range of
32-bit or 64-bit values, positive or negative.
Notice that there is no nextLong() call with an upper bound. Using the longs() method
allows you to generate a stream of longs between a given set of lower and upper bounds.
|
nextBoolean() |
Generates a random flag. |
In the specific case of the java.lang.Random() class, this will
take the highest-order bit of the underlying random number, which exhibits the highest degree
of randomness. Better quality generators do not have such a bias in the randomness of their bits. |
nextDouble() |
Generates a random double between 0 and 1 (exclusive). |
The result can be multiplied up to generate numbers between 0 and a different upper bound. Using the
doubles() method to generate a stream allows you to specify lower/upper bounds and includes
a floating point rounding correction. |
nextGaussian() |
Generates a random double with a normal distribution. |
See the separate discussion of the nextGaussian() method. |
ints() longs() doubles() |
Return a Stream that will generate random ints, longs or doubles. |
Versions of these methods are included that allow you to specify minimum and maximum bounds. These
make up for the absence of nextLong() and nextDouble() methods with upper bounds,
and include statistical corrections that might not be obvious if you were to simply take nextLong()
or nextDouble() and take the relevant multiple or modulus.
|