Package squidpony.squidmath
Class MersenneTwister
java.lang.Object
squidpony.squidmath.MersenneTwister
- All Implemented Interfaces:
Serializable
,RandomnessSource
public class MersenneTwister extends Object implements Serializable, RandomnessSource
Mersenne Twister, 64-bit version as a RandomnessSource.
Similar to the regular 32-bit Mersenne Twister but implemented with 64-bit values (Java
This is mostly a straight port of the C-code (mt19937-64.c), but made more "java-like". This version was originally found at an archived version of a Google Code repo, and it is licensed under the 3-clause BSD license, like the Mersenne Twister.
References:
Similar to the regular 32-bit Mersenne Twister but implemented with 64-bit values (Java
long
), and with different output. This generator is probably
not the best to use because of known statistical problems and low speed, but its period
is absurdly high, pow(2, 19937) - 1
. LongPeriodRNG
has significantly
better speed and statistical quality, and also has a large period, pow(2, 1024) - 1
.
IsaacRNG
is slower, but offers impeccable quality, and from its webpage, "Cycles
are guaranteed to be at least pow(2, 40)
values long, and they are
pow(2, 8295)
values long on average." IsaacRNG should be your choice if security is a
concern, LongPeriodRNG if quality and speed are important, and MersenneTwister should be used
if period is the only criterion to judge an RNG on. Keep in mind that extremely long periods
are not always a good thing; there are known states for the Mersenne Twister that produce dozens
of 0
outputs in a row, which is fundamentally impossible for LightRNG
or
DiverRNG
. It also would take longer to exhaust the period of a 128-bit-state generator
(generating 100 gigabytes per second) than the amount of time humans have walked the Earth.
This has 19968 bits of state... so if 128 is more than is possible to exhaust, why do you need
19968 bits, again? Consider GoatRNG
if you want a high-quality 128-bit generator.
This is mostly a straight port of the C-code (mt19937-64.c), but made more "java-like". This version was originally found at an archived version of a Google Code repo, and it is licensed under the 3-clause BSD license, like the Mersenne Twister.
References:
- The Mersenne Twister Home Page
- Makato Matsumoto and Takuji Nishimura, "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.
- Version:
- 1.1 -- 07-Oct-2017
- Author:
- Nick Galbreath -- nickg [at] modp [dot] com, Tommy Ettinger
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description MersenneTwister()
Seeds this using two calls to Math.random().MersenneTwister(long seed)
Seeds this with the given long, which will be used to affect the large state.MersenneTwister(long[] seed)
Seeds this with the given long array, which will be used to affect the large state, and not used directly. -
Method Summary
Modifier and Type Method Description MersenneTwister
copy()
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called.boolean
equals(Object o)
int
hashCode()
int
next(int bits)
Returns up to 32 random bits.long
nextLong()
Returns 64 random bits.void
setSeed(long seed)
Initalize the pseudo random number generator with 64 bits.void
setSeed(long[] array)
Initalize the pseudo random number generator with a long array of any size, which should not be null but can be.String
toString()
-
Constructor Details
-
MersenneTwister
public MersenneTwister()Seeds this using two calls to Math.random(). -
MersenneTwister
Seeds this with the given long, which will be used to affect the large state.- Parameters:
seed
- any long
-
MersenneTwister
Seeds this with the given long array, which will be used to affect the large state, and not used directly.- Parameters:
seed
- a long array; generally should be non-null
-
-
Method Details
-
setSeed
Initalize the pseudo random number generator with 64 bits. Not the same as setState() in StatefulRandomness; this changes the seed quite a bit.- Parameters:
seed
- any long
-
setSeed
Initalize the pseudo random number generator with a long array of any size, which should not be null but can be. Not the same as setState() in StatefulRandomness; this changes the seed quite a bit.- Parameters:
array
- any long array
-
next
Returns up to 32 random bits.
The implementation splits a 64-bit long into two 32-bit chunks.- Specified by:
next
in interfaceRandomnessSource
- Parameters:
bits
- the number of bits to output, between 1 and 32 (both inclusive)- Returns:
- an int with the specified number of pseudo-random bits
-
nextLong
Returns 64 random bits.- Specified by:
nextLong
in interfaceRandomnessSource
- Returns:
- a pseudo-random long, which can have any 64-bit value, positive or negative
-
copy
Description copied from interface:RandomnessSource
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called. This just needs to copy the state so it isn't shared, usually, and produce a new value with the same exact state.- Specified by:
copy
in interfaceRandomnessSource
- Returns:
- a copy of this RandomnessSource
-
toString
-
equals
-
hashCode
-