Using methods on java.lang.Random and other Java random number generation classes

In our introduction to random numbers in Java, we saw the example of how to simulate a dice roll using the nextInt() method. The java.lang.Random class and its subclasses define a range of other methods for generating random numbers of different types and distributions. As a rule of thumb, you sould always use the specific method for your purpose if it is available. They include some subtle corrections to improve the statistical distribution of the random numbers generated that you might not think of if you were implementing methods such as nextInt() from scratch.

The java.lang.Random class itself implements what is essentially a legacy algorithm that is not recommended for most applications. However, ThreadLocalRandom and the various other subclasses and Java random number generation classes will generally implement these methods. If you implement your own subclass of java.lang.Random by overriding the nextBits() method, you will also inherit the default implementations of these other methods:

MethodsUseNotes
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.

What to read next


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.