public class MersenneTwister extends java.lang.Object implements java.io.Serializable, RandomnessSource
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.
Constructor and 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.
|
Modifier and Type | Method and 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(java.lang.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.
|
java.lang.String |
toString() |
public MersenneTwister()
public MersenneTwister(long seed)
seed
- any longpublic MersenneTwister(long[] seed)
seed
- a long array; generally should be non-nullpublic void setSeed(long seed)
seed
- any longpublic void setSeed(long[] array)
array
- any long arraypublic int next(int bits)
next
in interface RandomnessSource
bits
- the number of bits to output, between 1 and 32 (both inclusive)public long nextLong()
nextLong
in interface RandomnessSource
public final MersenneTwister copy()
RandomnessSource
copy
in interface RandomnessSource
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
Copyright © Eben Howard 2012–2022. All rights reserved.