Uncommons Maths API
(Version 1.2.3)

Class MersenneTwisterRNG

  extended by java.util.Random
      extended by org.uncommons.maths.random.MersenneTwisterRNG
All Implemented Interfaces:
Serializable, RepeatableRNG

public class MersenneTwisterRNG
extends Random
implements RepeatableRNG

Random number generator based on the Mersenne Twister algorithm developed by Makoto Matsumoto and Takuji Nishimura.

This is a very fast random number generator with good statistical properties (it passes the full DIEHARD suite). This is the best RNG for most experiments. If a non-linear generator is required, use the slower AESCounterRNG RNG.

This PRNG is deterministic, which can be advantageous for testing purposes since the output is repeatable. If multiple instances of this class are created with the same seed they will all have identical output.

This code is translated from the original C version and assumes that we will always seed from an array of bytes. I don't pretend to know the meanings of the magic numbers or how it works, it just does.

NOTE: Because instances of this class require 128-bit seeds, it is not possible to seed this RNG using the Random.setSeed(long) method inherited from Random. Calls to this method will have no effect. Instead the seed must be set by a constructor.

Makoto Matsumoto and Takuji Nishimura (original C version), Daniel Dyer (Java port)
See Also:
Serialized Form

Constructor Summary
          Creates a new RNG and seeds it using the default seeding strategy.
MersenneTwisterRNG(byte[] seed)
          Creates an RNG and seeds it with the specified seed data.
MersenneTwisterRNG(SeedGenerator seedGenerator)
          Seed the RNG using the provided seed generation strategy.
Method Summary
 byte[] getSeed()
protected  int next(int bits)
Methods inherited from class java.util.Random
nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, setSeed
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public MersenneTwisterRNG()
Creates a new RNG and seeds it using the default seeding strategy.


public MersenneTwisterRNG(SeedGenerator seedGenerator)
                   throws SeedException
Seed the RNG using the provided seed generation strategy.

seedGenerator - The seed generation strategy that will provide the seed value for this RNG.
SeedException - If there is a problem generating a seed.


public MersenneTwisterRNG(byte[] seed)
Creates an RNG and seeds it with the specified seed data.

seed - The seed data used to initialise the RNG.
Method Detail


public byte[] getSeed()

Specified by:
getSeed in interface RepeatableRNG
The seed data used to initialise this pseudo-random number generator.


protected final int next(int bits)

next in class Random

Uncommons Maths API
(Version 1.2.3)