Version 1.2 (June 2013)
 All Classes Namespaces Files Functions Variables Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
utils.MTRandom Class Reference

An implementation of the original java mersenne twister class by Makoto Matsumoto and Takuji Nishimura. More...

Inheritance diagram for utils.MTRandom:

Public Member Functions

 MTRandom ()
 
 MTRandom (boolean compatible)
 
 MTRandom (long seed)
 
 MTRandom (byte[] buf)
 
 MTRandom (int[] buf)
 
final synchronized void setSeed (long seed)
 
final void setSeed (byte[] buf)
 
final synchronized void setSeed (int[] buf)
 

Static Public Member Functions

static int[] pack (byte[] buf)
 

Protected Member Functions

final synchronized int next (int bits)
 

Detailed Description

An implementation of the original java mersenne twister class by Makoto Matsumoto and Takuji Nishimura.

An implementation of the original java mersenne twister class by Makoto Matsumoto and Takuji Nishimura

Author
Chinmay Kanchi (CGK81.nosp@m.3@bh.nosp@m.am.ac.nosp@m..uk), Centre for Systems Biology, University of Birmingham (UK)

COMMENT ON ORIGINAL SOURCE CODE: MTRandom : 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. Author : David Beaumont Email : mersenne-at-www.goui.net

For the original C code, see: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

This version, Copyright (C) 2005, David Beaumont.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Version
1.0
Author
David Beaumont, Copyright 2005

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

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.

Note that the 4 transient int and int[] fields in the original code have commented out. These (obviously) prevented the MTRandom class from being serialized properly. I'm not entirely sure that this might not have knock-on effects, but it doesn't seem to.

Constructor & Destructor Documentation

utils.MTRandom.MTRandom ( )

The default constructor for an instance of MTRandom. This invokes the no-argument constructor for java.util.Random which will result in the class being initialised with a seed value obtained by calling System.currentTimeMillis().

utils.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.

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.

Parameters
compatibleCompatibility flag for replicating original behaviour.
utils.MTRandom.MTRandom ( long  seed)

This version of the constructor simply initialises the class with the given 64 bit seed value. For a better random number sequence this seed value should contain as much entropy as possible.

Parameters
seedThe seed value with which to initialise this class.
utils.MTRandom.MTRandom ( byte[]  buf)

This version of the constructor initialises the class with the given byte array. All the data will be used to initialise this instance.

Parameters
bufThe non-empty byte array of seed information.
Exceptions
NullPointerExceptionif the buffer is null.
IllegalArgumentExceptionif the buffer has zero length.
utils.MTRandom.MTRandom ( int[]  buf)

This version of the constructor initialises the class with the given integer array. All the data will be used to initialise this instance.

Parameters
bufThe non-empty integer array of seed information.
Exceptions
NullPointerExceptionif the buffer is null.
IllegalArgumentExceptionif the buffer has zero length.

Member Function Documentation

final synchronized int utils.MTRandom.next ( int  bits)
protected

This method forms the basis for generating a pseudo random number sequence from this class. If given a value of 32, this method behaves identically to the genrand_int32 function in the original C code and ensures that using the standard nextInt() function (inherited from Random) we are able to replicate behaviour exactly.

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);
Parameters
bitsThe number of significant bits desired in the output.
Returns
The next value in the pseudo random sequence with the specified number of bits in the lower part of the integer.
static int [] utils.MTRandom.pack ( byte[]  buf)
static

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. By packing the byte array into an integer array first, using this method, and then invoking setSeed() with that; it removes the need to re-pack the byte array each time setSeed() is called.

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.

Parameters
bufThe non-null byte array to be packed.
Returns
A non-null integer array of the packed bytes.
Exceptions
NullPointerExceptionif the given byte array is null.
final synchronized void utils.MTRandom.setSeed ( long  seed)

This method resets the state of this instance using the 64 bits of seed data provided. Note that if the same seed data is passed to two different instances of MTRandom (both of which share the same compatibility state) then the sequence of numbers generated by both instances will be identical.

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.

Parameters
seedThe 64 bit value used to initialise the random number generator state.
final void utils.MTRandom.setSeed ( byte[]  buf)

This method resets the state of this instance using the byte array of seed data provided. Note that calling this method is equivalent to calling "setSeed(pack(buf))" and in particular will result in a new integer array being generated during the call. If you wish to retain this seed data to allow the pseudo random sequence to be restarted then it would be more efficient to use the "pack()" method to convert it into an integer array first and then use that to re-seed the instance. The behaviour of the class will be the same in both cases but it will be more efficient.

Parameters
bufThe non-empty byte array of seed information.
Exceptions
NullPointerExceptionif the buffer is null.
IllegalArgumentExceptionif the buffer has zero length.
final synchronized void utils.MTRandom.setSeed ( int[]  buf)

This method resets the state of this instance using the integer array of seed data provided. This is the canonical way of resetting the pseudo random number sequence.

Parameters
bufThe non-empty integer array of seed information.
Exceptions
NullPointerExceptionif the buffer is null.
IllegalArgumentExceptionif the buffer has zero length.

The documentation for this class was generated from the following file: