Java tutorials home  java.util.Random  Random number generators  XORShift  High quality random  Seeding generators  Entropy  SecureRandom  Random sampling  Random simulations and nextGaussian()

Alternatives to java.util.Random

As discussed in our Java random numbers introduction, the JDK-provided java.util.Random class provides a low-quallity generator suitable for "informal" uses of random numbers. On this page, we give an overview of some alternatives that you should consider, depending on your requirements.

Random number generation techniques

There are various techniques that we could plug into java.util.Random instead of the standard algorithm.

Extremely fast, medium quality number generation

Since the Random class was originally designed, a technique was invented by mathematician George Marsaglia that can generate medium-quality random numbers extremely quickly, and using only a single variable as its state. The technique is generally known as an XORShift generator and is so simple that the code to generate numbers can often be inlined where you need the numbers generated. The technique is especially useful when you need to generate a large quantity of random numbers in a tight loop, or simply where you need a generator that is simple to code but with a slightly better quality and period than java.util.Random.

In Java, good candidates for using this generator include J2ME games.

Random numbers where you need good quality, fast generation

In applications where you need good— but not cryptographic— quality random numbers and need to generate a large number of them, then a Java implementation of the Numerical Recipes random number generator is often a reasonable choice. This is within the same order of magnitude of the speed of java.util.Random and generates numbers of a much higher quality. However, it still does not generate numbers of cryptographic quality (see the next section).

Random numbers if you're fussy about quality but not CPU usage

If you need high-quality random numbers "out of the box", for example for generating a random encryption key or for cases where you want to overcome some of the weaknesses of java.util.Random, then you can generally use java.security.SecureRandom as a drop-in replacement for java.util.Random:

Random diceRoller = new SecureRandom();
// proceed as before...

The SecureRandom implementation provides a much higher quality of randomness, and, with help from the OS, seeds itself using sources of entropy (or "true unpredictability") available on the local machine. However, it is about 20-30 times slower than other typical generators. (Note that "slower" still means you can produce about a million random longs in a second on a 2GHz processor!)

Subclassing java.util.Random

We should iterate that it is essentially the underlying algorithm of java.util.Random that we want to replace. The class itself still provides various useful methods, such as being able to take an arbitrary generator and from it produce random numbers with a normal distribution, useful for simulations.

This essentially means that, whatever technique we choose, we usually want to subclass java.util.Random, overriding its bit generation method but essentially nothing else.

comments powered by Disqus

Written by Neil Coffey. Copyright © Javamex UK 2013. All rights reserved.