com.buck.commons.algorithms
Class MersenneTwister

java.lang.Object
  extended by java.util.Random
      extended by com.buck.commons.algorithms.MersenneTwister
All Implemented Interfaces:
Serializable, Cloneable

public class MersenneTwister
extends Random
implements Cloneable, Serializable

See Also:
Serialized Form

Constructor Summary
MersenneTwister()
          Constructor using the default seed which is generated from system entropy.
MersenneTwister(byte[] bytes)
           
MersenneTwister(int[] array)
          Constructor using an array of integers as seed.
MersenneTwister(long seed)
          Constructor using a given seed.
 
Method Summary
 Object clone()
           
protected  int next(int bits)
          Returns an integer with bits bits filled with a random number.
 boolean nextBoolean()
          This method is missing from jdk 1.0.x and below.
 boolean nextBoolean(double probability)
          This generates a coin flip with a probability probability of returning true, else returning false.
 boolean nextBoolean(float probability)
          This generates a coin flip with a probability probability of returning true, else returning false.
 byte nextByte()
          For completeness' sake, though it's not in java.util.Random.
 void nextBytes(byte[] bytes)
          A bug fix for all versions of the JDK.
 char nextChar()
          For completeness' sake, though it's not in java.util.Random.
 double nextDouble()
          A bug fix for versions of JDK 1.1 and below.
 float nextFloat()
          A bug fix for versions of JDK 1.1 and below.
 double nextGaussian()
          A bug fix for all JDK code including 1.2.
 int nextInt(int n)
          This method is missing from JDK 1.1 and below.
 long nextLong(long n)
          This method is for completeness' sake.
 short nextShort()
          For completeness' sake, though it's not in java.util.Random.
 void readState(DataInputStream stream)
          Reads the entire state of the MersenneTwister RNG from the stream
 void setSeed(byte[] manifest)
           
 void setSeed(int[] array)
          Sets the seed of the MersenneTwister using an array of integers.
 void setSeed(long seed)
          Initalize the pseudo random number generator.
 boolean stateEquals(Object o)
           
 void writeState(DataOutputStream stream)
          Writes the entire state of the MersenneTwister RNG to the stream
 
Methods inherited from class java.util.Random
nextInt, nextLong
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MersenneTwister

public MersenneTwister()
Constructor using the default seed which is generated from system entropy.


MersenneTwister

public MersenneTwister(byte[] bytes)

MersenneTwister

public MersenneTwister(long seed)
Constructor using a given seed. Though you pass this seed in as a long, it's best to make sure it's actually an integer.

Parameters:
seed - the starting seed for the random number generator

MersenneTwister

public MersenneTwister(int[] array)
Constructor using an array of integers as seed. Your array must have a non-zero length. Only the first 624 integers in the array are used; if the array is shorter than this then integers are repeatedly used in a wrap-around fashion.

Parameters:
array - the array of integers to draw the seed from
Method Detail

clone

public Object clone()
             throws CloneNotSupportedException
Overrides:
clone in class Object
Throws:
CloneNotSupportedException

stateEquals

public boolean stateEquals(Object o)

readState

public void readState(DataInputStream stream)
               throws IOException
Reads the entire state of the MersenneTwister RNG from the stream

Parameters:
stream - to read from
Throws:
IOException - if the read fails

writeState

public void writeState(DataOutputStream stream)
                throws IOException
Writes the entire state of the MersenneTwister RNG to the stream

Parameters:
stream - the stream to write the RNG state to
Throws:
IOException - if the write fails

setSeed

public void setSeed(long seed)
Initalize the pseudo random number generator. Don't pass in a long that's bigger than an int (Mersenne Twister only uses the first 32 bits for its seed).

Overrides:
setSeed in class Random

setSeed

public void setSeed(byte[] manifest)

setSeed

public void setSeed(int[] array)
Sets the seed of the MersenneTwister using an array of integers. Your array must have a non-zero length. Only the first 624 integers in the array are used; if the array is shorter than this then integers are repeatedly used in a wrap-around fashion.

Parameters:
array - the array of seed values to draw from

next

protected int next(int bits)
Returns an integer with bits bits filled with a random number.

Overrides:
next in class Random

nextBoolean

public boolean nextBoolean()
This method is missing from jdk 1.0.x and below. JDK 1.1 includes this for us, but what the heck.

Overrides:
nextBoolean in class Random

nextBoolean

public boolean nextBoolean(float probability)
This generates a coin flip with a probability probability of returning true, else returning false. probability must be between 0.0 and 1.0, inclusive. Not as precise a random real event as nextBoolean(double), but twice as fast. To explicitly use this, remember you may need to cast to float first.

Parameters:
probability - the probability that the next call will return true
Returns:
the next boolean

nextBoolean

public boolean nextBoolean(double probability)
This generates a coin flip with a probability probability of returning true, else returning false. probability must be between 0.0 and 1.0, inclusive.

Parameters:
probability - the probability that the method returns true
Returns:
the next boolean

nextBytes

public void nextBytes(byte[] bytes)
A bug fix for all versions of the JDK. The JDK appears to use all four bytes in an integer as independent byte values! Totally wrong. I've submitted a bug report.

Overrides:
nextBytes in class Random

nextByte

public byte nextByte()
For completeness' sake, though it's not in java.util.Random.

Returns:
the next random byte

nextChar

public char nextChar()
For completeness' sake, though it's not in java.util.Random.

Returns:
the next random character

nextShort

public short nextShort()
For completeness' sake, though it's not in java.util.Random.

Returns:
the next random short

nextInt

public int nextInt(int n)
This method is missing from JDK 1.1 and below. JDK 1.2 includes this for us, but what the heck.

Overrides:
nextInt in class Random

nextLong

public long nextLong(long n)
This method is for completeness' sake. Returns a long drawn uniformly from 0 to n-1. Suffice it to say, n must be > 0, or an IllegalArgumentException is raised.

Parameters:
n - one greater than the maximum value
Returns:
the next long

nextFloat

public float nextFloat()
A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes this for us, but what the heck.

Overrides:
nextFloat in class Random

nextDouble

public double nextDouble()
A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes this for us, but what the heck.

Overrides:
nextDouble in class Random

nextGaussian

public double nextGaussian()
A bug fix for all JDK code including 1.2. nextGaussian can theoretically ask for the log of 0 and divide it by 0! See Java bug http://developer.java.sun.com/developer/bugParade/bugs/4254501.html

Overrides:
nextGaussian in class Random


Copyright © 2013. All Rights Reserved.