Top Description Interfaces Classes
[default module]

Package org.apache.commons.math3.random


Random number and random data generators.

Commons-math provides a few pseudo random number generators. The top level interface is RandomGenerator. It is implemented by three classes:

The JDK provided generator is a simple one that can be used only for very simple needs. The Mersenne Twister is a fast generator with very good properties well suited for Monte-Carlo simulation. It is equidistributed for generating vectors up to dimension 623 and has a huge period: 219937 - 1 (which is a Mersenne prime). This generator is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30. The WELL generators are a family of generators with period ranging from 2512 - 1 to 244497 - 1 (this last one is also a Mersenne prime) with even better properties than Mersenne Twister. These generators are described in a paper by François Panneton, Pierre L'Ecuyer and Makoto Matsumoto Improved Long-Period Generators Based on Linear Recurrences Modulo 2 ACM Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper are in wellrng-errata.txt.

For simple sampling, any of these generators is sufficient. For Monte-Carlo simulations the JDK generator does not have any of the good mathematical properties of the other generators, so it should be avoided. The Mersenne twister and WELL generators have equidistribution properties proven according to their bits pool size which is directly linked to their period (all of them have maximal period, i.e. a generator with size n pool has a period 2n-1). They also have equidistribution properties for 32 bits blocks up to s/32 dimension where s is their pool size. So WELL19937c for exemple is equidistributed up to dimension 623 (19937/32). This means a Monte-Carlo simulation generating a vector of n variables at each iteration has some guarantees on the properties of the vector as long as its dimension does not exceed the limit. However, since we use bits from two successive 32 bits generated integers to create one double, this limit is smaller when the variables are of type double. so for Monte-Carlo simulation where less the 16 doubles are generated at each round, WELL1024 may be sufficient. If a larger number of doubles are needed a generator with a larger pool would be useful.

The WELL generators are more modern then MersenneTwister (the paper describing than has been published in 2006 instead of 1998) and fix some of its (few) drawbacks. If initialization array contains many zero bits, MersenneTwister may take a very long time (several hundreds of thousands of iterations to reach a steady state with a balanced number of zero and one in its bits pool). So the WELL generators are better to escape zeroland as explained by the WELL generators creators. The Well19937a and Well44497a generator are not maximally equidistributed (i.e. there are some dimensions or bits blocks size for which they are not equidistributed). The Well512a, Well1024a, Well19937c and Well44497b are maximally equidistributed for blocks size up to 32 bits (they should behave correctly also for double based on more than 32 bits blocks, but equidistribution is not proven at these blocks sizes).

The MersenneTwister generator uses a 624 elements integer array, so it consumes less than 2.5 kilobytes. The WELL generators use 6 integer arrays with a size equal to the pool size, so for example the WELL44497b generator uses about 33 kilobytes. This may be important if a very large number of generator instances were used at the same time.

All generators are quite fast. As an example, here are some comparisons, obtained on a 64 bits JVM on a linux computer with a 2008 processor (AMD phenom Quad 9550 at 2.2 GHz). The generation rate for MersenneTwister was about 27 millions doubles per second (remember we generate two 32 bits integers for each double). Generation rates for other PRNG, relative to MersenneTwister:

Example of performances
Namegeneration rate (relative to MersenneTwister)
MersenneTwister1
JDKRandomGeneratorbetween 0.96 and 1.16
Well512abetween 0.85 and 0.88
Well1024abetween 0.63 and 0.73
Well19937abetween 0.70 and 0.71
Well19937cbetween 0.57 and 0.71
Well44497abetween 0.69 and 0.71
Well44497bbetween 0.65 and 0.71

So for most simulation problems, the better generators like Well19937c and Well44497b are probably very good choices.

Note that none of these generators are suitable for cryptography. They are devoted to simulation, and to generate very long series with strong properties on the series as a whole (equidistribution, no correlation ...). They do not attempt to create small series but with very strong properties of unpredictability as needed in cryptography.

Interface Summary

Modifier and TypeInterface and Description
public interface
NormalizedRandomGenerator

This interface represent a normalized random generator for scalars.

public interface
RandomData

Deprecated to be removed in 4.0. Use RandomDataGenerator directly
Random data generation utilities.
public interface
RandomGenerator

Interface extracted from java.util.Random.

public interface
RandomVectorGenerator

This interface represents a random generator for whole vectors.

Class Summary

Modifier and TypeClass and Description
public abstract class
AbstractRandomGenerator

Abstract class implementing the RandomGenerator interface.

public abstract class
AbstractWell

This abstract class implements the WELL class of pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public abstract class
BitsStreamGenerator

Base class for random number generators that generates bits streams.

public class
CorrelatedRandomVectorGenerator

A RandomVectorGenerator that generates vectors with with correlated components.

public class
EmpiricalDistribution

Represents an empirical probability distribution -- a probability distribution derived from observed data without making any assumptions about the functional form of the population distribution that the data come from.

public class
GaussianRandomGenerator

This class is a gaussian normalized random generator for scalars.

public class
HaltonSequenceGenerator

Implementation of a Halton sequence.

public class
ISAACRandom

ISAAC: a fast cryptographic pseudo-random number generator ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit random numbers.

public class
JDKRandomGenerator

Extension of java.util.Random to implement RandomGenerator.

public class
MersenneTwister

This class implements a powerful pseudo-random number generator developed by Makoto Matsumoto and Takuji Nishimura during 1996-1997.

public class
RandomAdaptor

Extension of java.util.Random wrapping a RandomGenerator.

public class
RandomDataGenerator

Deprecated as a consequence of RandomData being deprecated.
Implements the RandomData interface using a RandomGenerator instance to generate non-secure data and a java.security.SecureRandom instance to provide data for the nextSecureXxx methods.
public class
RandomDataImpl

Deprecated to be removed in 4.0. Use RandomDataGenerator instead
Generates random deviates and other random data using a RandomGenerator instance to generate non-secure data and a java.security.SecureRandom instance to provide data for the nextSecureXxx methods.
public class
RandomGeneratorFactory

Utilities for creating RandomGenerator instances.

public class
SobolSequenceGenerator

Implementation of a Sobol sequence.

public class
StableRandomGenerator

This class provides a stable normalized random generator.

public class
SynchronizedRandomGenerator

Any RandomGenerator implementation can be thread-safe if it is used through an instance of this class.

public class
UncorrelatedRandomVectorGenerator

A RandomVectorGenerator that generates vectors with uncorrelated components.

public class
UniformRandomGenerator

This class implements a normalized uniform random generator.

public class
UnitSphereRandomVectorGenerator

Generate random vectors isotropically located on the surface of a sphere.

public class
ValueServer

Generates values for use in simulation applications.

public class
Well1024a

This class implements the WELL1024a pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public class
Well19937a

This class implements the WELL19937a pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public class
Well19937c

This class implements the WELL19937c pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public class
Well44497a

This class implements the WELL44497a pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public class
Well44497b

This class implements the WELL44497b pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

public class
Well512a

This class implements the WELL512a pseudo-random number generator from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.