Class MersenneTwisterRNG
- java.lang.Object
-
- java.util.Random
-
- org.uncommons.maths.random.MersenneTwisterRNG
-
- All Implemented Interfaces:
java.io.Serializable
,RepeatableRNG
public class MersenneTwisterRNG extends java.util.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 fromRandom
. Calls to this method will have no effect. Instead the seed must be set by a constructor.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BOOTSTRAP_FACTOR
private static int
BOOTSTRAP_SEED
private static int
GENERATE_MASK1
private static int
GENERATE_MASK2
private java.util.concurrent.locks.ReentrantLock
lock
private static int
LOWER_MASK
private static int
M
private static int[]
MAG01
private int[]
mt
private int
mtIndex
private static int
N
private byte[]
seed
private static int
SEED_FACTOR1
private static int
SEED_FACTOR2
private static int
SEED_SIZE_BYTES
private static int
UPPER_MASK
-
Constructor Summary
Constructors Constructor Description MersenneTwisterRNG()
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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
getSeed()
protected int
next(int bits)
-
-
-
Field Detail
-
SEED_SIZE_BYTES
private static final int SEED_SIZE_BYTES
- See Also:
- Constant Field Values
-
N
private static final int N
- See Also:
- Constant Field Values
-
M
private static final int M
- See Also:
- Constant Field Values
-
MAG01
private static final int[] MAG01
-
UPPER_MASK
private static final int UPPER_MASK
- See Also:
- Constant Field Values
-
LOWER_MASK
private static final int LOWER_MASK
- See Also:
- Constant Field Values
-
BOOTSTRAP_SEED
private static final int BOOTSTRAP_SEED
- See Also:
- Constant Field Values
-
BOOTSTRAP_FACTOR
private static final int BOOTSTRAP_FACTOR
- See Also:
- Constant Field Values
-
SEED_FACTOR1
private static final int SEED_FACTOR1
- See Also:
- Constant Field Values
-
SEED_FACTOR2
private static final int SEED_FACTOR2
- See Also:
- Constant Field Values
-
GENERATE_MASK1
private static final int GENERATE_MASK1
- See Also:
- Constant Field Values
-
GENERATE_MASK2
private static final int GENERATE_MASK2
- See Also:
- Constant Field Values
-
seed
private final byte[] seed
-
lock
private final java.util.concurrent.locks.ReentrantLock lock
-
mt
private final int[] mt
-
mtIndex
private int mtIndex
-
-
Constructor Detail
-
MersenneTwisterRNG
public MersenneTwisterRNG()
Creates a new RNG and seeds it using the default seeding strategy.
-
MersenneTwisterRNG
public MersenneTwisterRNG(SeedGenerator seedGenerator) throws SeedException
Seed the RNG using the provided seed generation strategy.- Parameters:
seedGenerator
- The seed generation strategy that will provide the seed value for this RNG.- Throws:
SeedException
- If there is a problem generating a seed.
-
MersenneTwisterRNG
public MersenneTwisterRNG(byte[] seed)
Creates an RNG and seeds it with the specified seed data.- Parameters:
seed
- The seed data used to initialise the RNG.
-
-
Method Detail
-
getSeed
public byte[] getSeed()
- Specified by:
getSeed
in interfaceRepeatableRNG
- Returns:
- The seed data used to initialise this pseudo-random number generator.
-
next
protected final int next(int bits)
- Overrides:
next
in classjava.util.Random
-
-