Class MersenneTwisterRandom
- java.lang.Object
-
- java.util.Random
-
- io.github.pr0methean.betterrandom.prng.BaseRandom
-
- io.github.pr0methean.betterrandom.prng.MersenneTwisterRandom
-
- All Implemented Interfaces:
ByteArrayReseedableRandom,EntropyCountingRandom,RepeatableRandom,Dumpable,Serializable
public class MersenneTwisterRandom extends BaseRandom
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
AesCounterRandomRNG.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
setSeed(long)method inherited fromRandom. Calls to this method will have no effect. Instead the seed must be set by a constructor.- Author:
- Makoto Matsumoto and Takuji Nishimura (original C version), Daniel Dyer (Java port)
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
ENTROPY_OF_DOUBLE, ENTROPY_OF_FLOAT, entropyBits, lock, randomSeeder, seed, superConstructorFinished
-
-
Constructor Summary
Constructors Constructor Description MersenneTwisterRandom()Creates a new RNG and seeds it using the default seeding strategy.MersenneTwisterRandom(byte[] seed)Creates an RNG and seeds it with the specified seed data.MersenneTwisterRandom(SeedGenerator seedGenerator)Seed the RNG using the provided seed generation strategy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MoreObjects.ToStringHelperaddSubclassFields(MoreObjects.ToStringHelper original)Adds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelperfor dumping.intgetNewSeedLength()Returns the only supported seed length.protected voidinitTransientFields()Called in constructor and readObject to initialize transient fields.protected intnext(int bits)Generates the next pseudorandom number.voidsetSeed(long seed)Reseeds this PRNG using theDefaultSeedGenerator, since it needs a longer seed.protected voidsetSeedInternal(byte[] seed)Sets the seed, and should be overridden to set other state that derives from the seed.-
Methods inherited from class io.github.pr0methean.betterrandom.prng.BaseRandom
checkLength, checkValidRange, creditEntropyForNewSeed, debitEntropy, doubles, doubles, doubles, doubles, dump, entropyOfInt, entropyOfLong, fallbackSetSeedIfInitialized, gaussians, gaussians, getEntropyBits, getRandomSeeder, getSeed, internalNextGaussian, ints, ints, ints, ints, lockForNextGaussian, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextDoubleNoEntropyDebit, nextElement, nextElement, nextEnum, nextFloat, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong, nextLong, nextLongNoEntropyDebit, preferSeedWithLong, setRandomSeeder, setSeed, supportsMultipleSeedLengths, unlockForNextGaussian, usesParallelStreams, withProbability, withProbabilityInternal
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.EntropyCountingRandom
needsReseedingEarly
-
-
-
-
Constructor Detail
-
MersenneTwisterRandom
public MersenneTwisterRandom() throws SeedExceptionCreates a new RNG and seeds it using the default seeding strategy.- Throws:
SeedException- if any.
-
MersenneTwisterRandom
public MersenneTwisterRandom(byte[] seed)
Creates an RNG and seeds it with the specified seed data.- Parameters:
seed- 16 bytes of seed data used to initialize the RNG.
-
MersenneTwisterRandom
public MersenneTwisterRandom(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.
-
-
Method Detail
-
addSubclassFields
protected MoreObjects.ToStringHelper addSubclassFields(MoreObjects.ToStringHelper original)
Description copied from class:BaseRandomAdds the fields that were not inherited from BaseRandom to the givenMoreObjects.ToStringHelperfor dumping.- Specified by:
addSubclassFieldsin classBaseRandom- Parameters:
original- aMoreObjects.ToStringHelperobject.- Returns:
originalwith the fields not inherited from BaseRandom written to it.
-
setSeed
public void setSeed(long seed)
Reseeds this PRNG using theDefaultSeedGenerator, since it needs a longer seed.- Overrides:
setSeedin classBaseRandom- Parameters:
seed- ignored- See Also:
ByteArrayReseedableRandom.preferSeedWithLong()
-
initTransientFields
protected void initTransientFields()
Description copied from class:BaseRandomCalled in constructor and readObject to initialize transient fields.- Overrides:
initTransientFieldsin classBaseRandom
-
setSeedInternal
protected void setSeedInternal(byte[] seed)
Description copied from class:BaseRandomSets the seed, and should be overridden to set other state that derives from the seed. Called byBaseRandom.setSeed(byte[]), constructors, andreadObject(ObjectInputStream). When called after initialization, theBaseRandom.lockis always held.- Overrides:
setSeedInternalin classBaseRandom- Parameters:
seed- The new seed.
-
next
protected final int next(int bits)
Description copied from class:BaseRandomGenerates the next pseudorandom number. Called by all other random-number-generating methods. Should not debit the entropy count, since that's done by the calling methods according to the amount they actually output (see for exampleBaseRandom.withProbability(double), which uses 53 random bits but outputs only one, and thus debits only 1 bit of entropy).- Specified by:
nextin classBaseRandom
-
getNewSeedLength
public int getNewSeedLength()
Returns the only supported seed length.- Specified by:
getNewSeedLengthin interfaceByteArrayReseedableRandom- Specified by:
getNewSeedLengthin classBaseRandom- Returns:
- The desired length of a new byte-array seed.
-
-