- All Implemented Interfaces:
public abstract class Randomextends Objectimplements SerializableClass Random is the abstract base class for a pseudorandom number generator (PRNG) designed for use in parallel scientific programming. It differs from class java.util.Random in the following ways:
- Instances can be created by calling a static factory method. The factory method can take the name of a subclass as an argument and return an instance of that subclass of class Random. This makes it easier to write a program that can substitute a different PRNG algorithm at run time. Class DefaultRandom provides a default PRNG algorithm.
- Whereas class java.util.Random generates 48-bit numbers under the hood, class Random generates 64-bit numbers. This makes class Random faster than class java.util.Random for some operations, notably the nextDouble() method.
- Whereas the PRNG algorithm in class java.util.Random has a period of about 248, the default PRNG algorithm in class DefaultRandom has a period of about 264. This lets a parallel program scale up to larger problem sizes without exhausting the PRNG's period.
- To support the leapfrogging and sequence splitting techniques for generating pseudorandom numbers in multiple parallel threads or processes, class Random includes methods for skipping ahead in the sequence of generated numbers, without having to generate all the intermediate numbers.
- To avoid unnecessary thread synchronization overhead, class Random is not multiple thread safe. It assumes that the surrounding program ensures that only one thread at a time calls methods on an instance.
Each method for generating a number comes in two versions; for example:
public double nextDouble(); public double nextDouble (long skip);Calling the second version with an argument skip is equivalent to calling the first version skip times:
Random prng1 = Random.getInstance (1234L); double x = prng1.nextDouble (1000); Random prng2 = Random.getInstance (1234L); double y; for (int i = 0; i < 1000; ++ i) y = prng2.nextDouble();At the end of the above code fragment, x and y will have the same value. However, calling nextDouble(1000) will typically be much faster than calling nextDouble() 1000 times. Conversely, nextDouble(1) is equivalent to nextDouble(), but the former will typically be slower than the latter.
An instance of class Random can be serialized; for example, to checkpoint the state of the PRNG into a file and restore its state later.
The design of class Random is inspired in part by Coddington's JAPARA library. For further information, see P. Coddington and A. Newell, JAPARA -- A Java random number generator library for high-performance computing, in Proceedings of the 18th IEEE International Parallel and Distributed Processing Symposium (IPDPS'04), April 26-30, 2004, page 156.
- See Also:
- Serialized Form