Package jasima.core.util
Class MersenneTwister
- java.lang.Object
-
- java.util.Random
-
- jasima.core.util.MersenneTwister
-
- All Implemented Interfaces:
Serializable,Cloneable
public class MersenneTwister extends Random implements Serializable, Cloneable
Copyright (c) 2010-2015 Torsten Hildebrandt and jasima contributors This file is part of jasima, v1.2. jasima is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jasima 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 General Public License for more details. You should have received a copy of the GNU General Public License along with jasima. If not, see http://www.gnu.org/licenses/.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MersenneTwister()Constructor using the default seed.MersenneTwister(int[] array)Constructor using an array of integers as seed.MersenneTwister(long seed)Constructor using a given seed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectclone()protected intnext(int bits)Returns an integer with bits bits filled with a random number.booleannextBoolean()This method is missing from jdk 1.0.x and below.booleannextBoolean(double probability)This generates a coin flip with a probability probability of returning true, else returning false.booleannextBoolean(float probability)This generates a coin flip with a probability probability of returning true, else returning false.bytenextByte()For completeness' sake, though it's not in java.util.Random.voidnextBytes(byte[] bytes)A bug fix for all versions of the JDK.charnextChar()For completeness' sake, though it's not in java.util.Random.doublenextDouble()A bug fix for versions of JDK 1.1 and below.floatnextFloat()A bug fix for versions of JDK 1.1 and below.doublenextGaussian()A bug fix for all JDK code including 1.2. nextGaussian can theoretically ask for the log of 0 and divide it by 0!intnextInt(int n)This method is missing from JDK 1.1 and below.longnextLong(long n)This method is for completness' sake.shortnextShort()For completeness' sake, though it's not in java.util.Random.voidreadState(DataInputStream stream)Reads the entire state of the MersenneTwister RNG from the stream.voidsetSeed(int[] array)Sets the seed of the MersenneTwister using an array of integers.voidsetSeed(long seed)Initalize the pseudo random number generator.booleanstateEquals(Object o)voidwriteState(DataOutputStream stream)Writes the entire state of the MersenneTwister RNG to the stream.
-
-
-
Constructor Detail
-
MersenneTwister
public MersenneTwister()
Constructor using the default seed.
-
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.
-
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.
-
-
Method Detail
-
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- The input stream to use.- Throws:
IOException
-
writeState
public void writeState(DataOutputStream stream) throws IOException
Writes the entire state of the MersenneTwister RNG to the stream.- Parameters:
stream- The output stream to use.- Throws:
IOException
-
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).
-
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.
-
next
protected int next(int bits)
Returns an integer with bits bits filled with a random number.
-
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:
nextBooleanin classRandom
-
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 of returningtrue.- Returns:
- A random bool value.
-
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 of returningtrue.- Returns:
- A random bool value.
-
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.
-
nextLong
public long nextLong(long n)
This method is for completness' sake. Returns a long drawn uniformly from 0 to n-1. Suffice it to say, n must be %gt; 0, or an IllegalArgumentException is raised.- Parameters:
n- The maximum value to return plus one.- Returns:
- A random long in the range [0,n-1].
-
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:
nextDoublein classRandom
-
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.
-
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.
-
nextChar
public char nextChar()
For completeness' sake, though it's not in java.util.Random.
-
nextShort
public short nextShort()
For completeness' sake, though it's not in java.util.Random.
-
nextByte
public byte nextByte()
For completeness' sake, though it's not in java.util.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:
nextGaussianin classRandom
-
-