public class MTRandom extends Random
A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm based upon the original C code by Makoto Matsumoto and Takuji Nishimura (see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html for more information.
As a subclass of java.util.Random this class provides a single canonical method next() for generating bits in the pseudo random number sequence. Anyone using this class should invoke the public inherited methods (nextInt(), nextFloat etc.) to obtain values as normal. This class should provide a drop-in replacement for the standard implementation of java.util.Random with the additional advantage of having a far longer period and the ability to use a far larger seed value.
This is not a cryptographically strong source of randomness and should not be used for cryptographic systems or in any other situation where true random numbers are required.
This software is licensed under the CC-GNU LGPL.
Constructor and Description |
---|
MTRandom()
The default constructor for an instance of MTRandom.
|
MTRandom(boolean compatible)
This version of the constructor can be used to implement identical
behaviour to the original C code version of this algorithm including
exactly replicating the case where the seed value had not been set
prior to calling genrand_int32.
|
MTRandom(byte[] buf)
This version of the constructor initialises the class with the
given byte array.
|
MTRandom(int[] buf)
This version of the constructor initialises the class with the
given integer array.
|
MTRandom(long seed)
This version of the constructor simply initialises the class with
the given 64 bit seed value.
|
Modifier and Type | Method and Description |
---|---|
protected int |
next(int bits)
This method forms the basis for generating a pseudo random number
sequence from this class.
|
static int[] |
pack(byte[] buf)
This simply utility method can be used in cases where a byte
array of seed data is to be used to repeatedly re-seed the
random number sequence.
|
void |
setSeed(byte[] buf)
This method resets the state of this instance using the byte
array of seed data provided.
|
void |
setSeed(int[] buf)
This method resets the state of this instance using the integer
array of seed data provided.
|
void |
setSeed(long seed)
This method resets the state of this instance using the 64
bits of seed data provided.
|
nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
public MTRandom()
public MTRandom(boolean compatible)
If the compatibility flag is set to true, then the algorithm will be seeded with the same default value as was used in the original C code. Furthermore the setSeed() method, which must take a 64 bit long value, will be limited to using only the lower 32 bits of the seed to facilitate seamless migration of existing C code into Java where identical behaviour is required.
Whilst useful for ensuring backwards compatibility, it is advised that this feature not be used unless specifically required, due to the reduction in strength of the seed value.
compatible
- Compatibility flag for replicating original
behaviour.public MTRandom(long seed)
seed
- The seed value with which to initialise this class.public MTRandom(byte[] buf)
buf
- The non-empty byte array of seed information.NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.public MTRandom(int[] buf)
buf
- The non-empty integer array of seed information.NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.public final void setSeed(long seed)
If this instance was initialised in 'compatibility' mode then this method will only use the lower 32 bits of any seed value passed in and will match the behaviour of the original C code exactly with respect to state initialisation.
public final void setSeed(byte[] buf)
buf
- The non-empty byte array of seed information.NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.public final void setSeed(int[] buf)
buf
- The non-empty integer array of seed information.NullPointerException
- if the buffer is null.IllegalArgumentException
- if the buffer has zero length.protected final int next(int bits)
Note that where the number of bits requested is not equal to 32 then bits will simply be masked out from the top of the returned integer value. That is to say that:
mt.setSeed(12345); int foo = mt.nextInt(16) + (mt.nextInt(16) << 16);will not give the same result as
mt.setSeed(12345); int foo = mt.nextInt(32);
public static int[] pack(byte[] buf)
If the length of the byte array is not a multiple of 4 then it is implicitly padded with zeros as necessary. For example:
byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }becomes
int[] { 0x04030201, 0x00000605 }
Note that this method will not complain if the given byte array is empty and will produce an empty integer array, but the setSeed() method will throw an exception if the empty integer array is passed to it.
buf
- The non-null byte array to be packed.NullPointerException
- if the given byte array is null.Copyright © 2014 University of Waikato, Hamilton, NZ. All Rights Reserved.