Class 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.
    • 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).
        Overrides:
        setSeed in class Random
      • 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.
        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 of returning true.
        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 returning true.
        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.
        Overrides:
        nextInt in class Random
      • 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:
        nextDouble in class Random
      • 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
      • 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
      • 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.