Advertisement
Chiddix

MersenneTwisterFast

Sep 15th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 45.86 KB | None | 0 0
  1. package ec.util;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.Serializable;
  7. import java.util.Random;
  8.  
  9. /**
  10.  * <h3>MersenneTwister and MersenneTwisterFast</h3>
  11.  * <p>
  12.  * <b>Version 20</b>, based on version MT199937(99/10/29) of the Mersenne
  13.  * Twister algorithm found at <a
  14.  * href="http://www.math.keio.ac.jp/matumoto/emt.html"> The Mersenne Twister
  15.  * Home Page</a>, with the initialization improved using the new 2002/1/26
  16.  * initialization algorithm By Sean Luke, October 2004.
  17.  *
  18.  * <p>
  19.  * <b>MersenneTwister</b> is a drop-in subclass replacement for
  20.  * java.util.Random. It is properly synchronized and can be used in a
  21.  * multithreaded environment. On modern VMs such as HotSpot, it is approximately
  22.  * 1/3 slower than java.util.Random.
  23.  *
  24.  * <p>
  25.  * <b>MersenneTwisterFast</b> is not a subclass of java.util.Random. It has the
  26.  * same public methods as Random does, however, and it is algorithmically
  27.  * identical to MersenneTwister. MersenneTwisterFast has hard-code inlined all
  28.  * of its methods directly, and made all of them final (well, the ones of
  29.  * consequence anyway). Further, these methods are <i>not</i> synchronized, so
  30.  * the same MersenneTwisterFast instance cannot be shared by multiple threads.
  31.  * But all this helps MersenneTwisterFast achieve well over twice the speed of
  32.  * MersenneTwister. java.util.Random is about 1/3 slower than
  33.  * MersenneTwisterFast.
  34.  *
  35.  * <h3>About the Mersenne Twister</h3>
  36.  * <p>
  37.  * This is a Java version of the C-program for MT19937: Integer version. The
  38.  * MT19937 algorithm was created by Makoto Matsumoto and Takuji Nishimura, who
  39.  * ask: "When you use this, send an email to: matumoto@math.keio.ac.jp with an
  40.  * appropriate reference to your work". Indicate that this is a translation of
  41.  * their algorithm into Java.
  42.  *
  43.  * <p>
  44.  * <b>Reference. </b> Makato Matsumoto and Takuji Nishimura, "Mersenne Twister:
  45.  * A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator",
  46.  * <i>ACM Transactions on Modeling and. Computer Simulation,</i> Vol. 8, No. 1,
  47.  * January 1998, pp 3--30.
  48.  *
  49.  * <h3>About this Version</h3>
  50.  *
  51.  * <p>
  52.  * <b>Changes since V19:</b> nextFloat(boolean, boolean) now returns float, not
  53.  * double.
  54.  *
  55.  * <p>
  56.  * <b>Changes since V18:</b> Removed old final declarations, which used to
  57.  * potentially speed up the code, but no longer.
  58.  *
  59.  * <p>
  60.  * <b>Changes since V17:</b> Removed vestigial references to &= 0xffffffff which
  61.  * stemmed from the original C code. The C code could not guarantee that ints
  62.  * were 32 bit, hence the masks. The vestigial references in the Java code were
  63.  * likely optimized out anyway.
  64.  *
  65.  * <p>
  66.  * <b>Changes since V16:</b> Added nextDouble(includeZero, includeOne) and
  67.  * nextFloat(includeZero, includeOne) to allow for half-open, fully-closed, and
  68.  * fully-open intervals.
  69.  *
  70.  * <p>
  71.  * <b>Changes Since V15:</b> Added serialVersionUID to quiet compiler warnings
  72.  * from Sun's overly verbose compilers as of JDK 1.5.
  73.  *
  74.  * <p>
  75.  * <b>Changes Since V14:</b> made strictfp, with StrictMath.log and
  76.  * StrictMath.sqrt in nextGaussian instead of Math.log and Math.sqrt. This is
  77.  * largely just to be safe, as it presently makes no difference in the speed,
  78.  * correctness, or results of the algorithm.
  79.  *
  80.  * <p>
  81.  * <b>Changes Since V13:</b> clone() method CloneNotSupportedException removed.
  82.  *
  83.  * <p>
  84.  * <b>Changes Since V12:</b> clone() method added.
  85.  *
  86.  * <p>
  87.  * <b>Changes Since V11:</b> stateEquals(...) method added. MersenneTwisterFast
  88.  * is equal to other MersenneTwisterFasts with identical state; likewise
  89.  * MersenneTwister is equal to other MersenneTwister with identical state. This
  90.  * isn't equals(...) because that requires a contract of immutability to compare
  91.  * by value.
  92.  *
  93.  * <p>
  94.  * <b>Changes Since V10:</b> A documentation error suggested that setSeed(int[])
  95.  * required an int[] array 624 long. In fact, the array can be any non-zero
  96.  * length. The new version also checks for this fact.
  97.  *
  98.  * <p>
  99.  * <b>Changes Since V9:</b> readState(stream) and writeState(stream) provided.
  100.  *
  101.  * <p>
  102.  * <b>Changes Since V8:</b> setSeed(int) was only using the first 28 bits of the
  103.  * seed; it should have been 32 bits. For small-number seeds the behavior is
  104.  * identical.
  105.  *
  106.  * <p>
  107.  * <b>Changes Since V7:</b> A documentation error in MersenneTwisterFast (but
  108.  * not MersenneTwister) stated that nextDouble selects uniformly from the
  109.  * full-open interval [0,1]. It does not. nextDouble's contract is identical
  110.  * across MersenneTwisterFast, MersenneTwister, and java.util.Random, namely,
  111.  * selection in the half-open interval [0,1). That is, 1.0 should not be
  112.  * returned. A similar contract exists in nextFloat.
  113.  *
  114.  * <p>
  115.  * <b>Changes Since V6:</b> License has changed from LGPL to BSD. New timing
  116.  * information to compare against java.util.Random. Recent versions of HotSpot
  117.  * have helped Random increase in speed to the point where it is faster than
  118.  * MersenneTwister but slower than MersenneTwisterFast (which should be the
  119.  * case, as it's a less complex algorithm but is synchronized).
  120.  *
  121.  * <p>
  122.  * <b>Changes Since V5:</b> New empty constructor made to work the same as
  123.  * java.util.Random -- namely, it seeds based on the current time in
  124.  * milliseconds.
  125.  *
  126.  * <p>
  127.  * <b>Changes Since V4:</b> New initialization algorithms. See (see <a
  128.  * href="http://www.math.keio.ac.jp/matumoto/MT2002/emt19937ar.html"</a>
  129.  * http://www.math.keio.ac.jp/matumoto/MT2002/emt19937ar.html</a>)
  130.  *
  131.  * <p>
  132.  * The MersenneTwister code is based on standard MT19937 C/C++ code by Takuji
  133.  * Nishimura, with suggestions from Topher Cooper and Marc Rieffel, July 1997.
  134.  * The code was originally translated into Java by Michael Lecuyer, January
  135.  * 1999, and the original code is Copyright (c) 1999 by Michael Lecuyer.
  136.  *
  137.  * <h3>Java notes</h3>
  138.  *
  139.  * <p>
  140.  * This implementation implements the bug fixes made in Java 1.2's version of
  141.  * Random, which means it can be used with earlier versions of Java. See <a
  142.  * href=
  143.  * "http://www.javasoft.com/products/jdk/1.2/docs/api/java/util/Random.html">
  144.  * the JDK 1.2 java.util.Random documentation</a> for further documentation on
  145.  * the random-number generation contracts made. Additionally, there's an
  146.  * undocumented bug in the JDK java.util.Random.nextBytes() method, which this
  147.  * code fixes.
  148.  *
  149.  * <p>
  150.  * Just like java.util.Random, this generator accepts a long seed but doesn't
  151.  * use all of it. java.util.Random uses 48 bits. The Mersenne Twister instead
  152.  * uses 32 bits (int size). So it's best if your seed does not exceed the int
  153.  * range.
  154.  *
  155.  * <p>
  156.  * MersenneTwister can be used reliably on JDK version 1.1.5 or above. Earlier
  157.  * Java versions have serious bugs in java.util.Random; only MersenneTwisterFast
  158.  * (and not MersenneTwister nor java.util.Random) should be used with them.
  159.  *
  160.  * <h3>License</h3>
  161.  *
  162.  * Copyright (c) 2003 by Sean Luke. <br>
  163.  * Portions copyright (c) 1993 by Michael Lecuyer. <br>
  164.  * All rights reserved. <br>
  165.  *
  166.  * <p>
  167.  * Redistribution and use in source and binary forms, with or without
  168.  * modification, are permitted provided that the following conditions are met:
  169.  * <ul>
  170.  * <li>Redistributions of source code must retain the above copyright notice,
  171.  * this list of conditions and the following disclaimer.
  172.  * <li>Redistributions in binary form must reproduce the above copyright notice,
  173.  * this list of conditions and the following disclaimer in the documentation
  174.  * and/or other materials provided with the distribution.
  175.  * <li>Neither the name of the copyright owners, their employers, nor the names
  176.  * of its contributors may be used to endorse or promote products derived from
  177.  * this software without specific prior written permission.
  178.  * </ul>
  179.  * <p>
  180.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  181.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  182.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  183.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR CONTRIBUTORS BE
  184.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  185.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  186.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  187.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  188.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  189.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  190.  * POSSIBILITY OF SUCH DAMAGE.
  191.  *
  192.  * @version 20
  193.  */
  194.  
  195. // Note: this class is hard-inlined in all of its methods. This makes some of
  196. // the methods well-nigh unreadable in their complexity. In fact, the Mersenne
  197. // Twister is fairly easy code to understand: if you're trying to get a handle
  198. // on the code, I strongly suggest looking at MersenneTwister.java first.
  199. // -- Sean
  200.  
  201. public strictfp class MersenneTwisterFast implements Serializable, Cloneable {
  202.     // Serialization
  203.     private static final long serialVersionUID = -8219700664442619525L; // locked
  204.                                                                         // as of
  205.                                                                         // Version
  206.                                                                         // 15
  207.  
  208.     // Period parameters
  209.     private static final int N = 624;
  210.     private static final int M = 397;
  211.     private static final int MATRIX_A = 0x9908b0df; // private static final *
  212.                                                     // constant vector a
  213.     private static final int UPPER_MASK = 0x80000000; // most significant w-r
  214.                                                         // bits
  215.     private static final int LOWER_MASK = 0x7fffffff; // least significant r
  216.                                                         // bits
  217.  
  218.     // Tempering parameters
  219.     private static final int TEMPERING_MASK_B = 0x9d2c5680;
  220.     private static final int TEMPERING_MASK_C = 0xefc60000;
  221.  
  222.     private int mt[]; // the array for the state vector
  223.     private int mti; // mti==N+1 means mt[N] is not initialized
  224.     private int mag01[];
  225.  
  226.     // a good initial seed (of int size, though stored in a long)
  227.     // private static final long GOOD_SEED = 4357;
  228.  
  229.     private double __nextNextGaussian;
  230.     private boolean __haveNextNextGaussian;
  231.  
  232.     /*
  233.      * We're overriding all internal data, to my knowledge, so this should be
  234.      * okay
  235.      */
  236.     @Override
  237.     public Object clone() {
  238.         try {
  239.             final MersenneTwisterFast f = (MersenneTwisterFast) (super.clone());
  240.             f.mt = (mt.clone());
  241.             f.mag01 = (mag01.clone());
  242.             return f;
  243.         } catch (final CloneNotSupportedException e) {
  244.             throw new InternalError();
  245.         } // should never happen
  246.     }
  247.  
  248.     public boolean stateEquals(final Object o) {
  249.         if (o == this) {
  250.             return true;
  251.         }
  252.         if (o == null || !(o instanceof MersenneTwisterFast)) {
  253.             return false;
  254.         }
  255.         final MersenneTwisterFast other = (MersenneTwisterFast) o;
  256.         if (mti != other.mti) {
  257.             return false;
  258.         }
  259.         for (int x = 0; x < mag01.length; x++) {
  260.             if (mag01[x] != other.mag01[x]) {
  261.                 return false;
  262.             }
  263.         }
  264.         for (int x = 0; x < mt.length; x++) {
  265.             if (mt[x] != other.mt[x]) {
  266.                 return false;
  267.             }
  268.         }
  269.         return true;
  270.     }
  271.  
  272.     /** Reads the entire state of the MersenneTwister RNG from the stream */
  273.     public void readState(final DataInputStream stream) throws IOException {
  274.         int len = mt.length;
  275.         for (int x = 0; x < len; x++) {
  276.             mt[x] = stream.readInt();
  277.         }
  278.  
  279.         len = mag01.length;
  280.         for (int x = 0; x < len; x++) {
  281.             mag01[x] = stream.readInt();
  282.         }
  283.  
  284.         mti = stream.readInt();
  285.         __nextNextGaussian = stream.readDouble();
  286.         __haveNextNextGaussian = stream.readBoolean();
  287.     }
  288.  
  289.     /** Writes the entire state of the MersenneTwister RNG to the stream */
  290.     public void writeState(final DataOutputStream stream) throws IOException {
  291.         int len = mt.length;
  292.         for (int x = 0; x < len; x++) {
  293.             stream.writeInt(mt[x]);
  294.         }
  295.  
  296.         len = mag01.length;
  297.         for (int x = 0; x < len; x++) {
  298.             stream.writeInt(mag01[x]);
  299.         }
  300.  
  301.         stream.writeInt(mti);
  302.         stream.writeDouble(__nextNextGaussian);
  303.         stream.writeBoolean(__haveNextNextGaussian);
  304.     }
  305.  
  306.     /**
  307.      * Constructor using the default seed.
  308.      */
  309.     public MersenneTwisterFast() {
  310.         this(System.currentTimeMillis());
  311.     }
  312.  
  313.     /**
  314.      * Constructor using a given seed. Though you pass this seed in as a long,
  315.      * it's best to make sure it's actually an integer.
  316.      *
  317.      */
  318.     public MersenneTwisterFast(final long seed) {
  319.         setSeed(seed);
  320.     }
  321.  
  322.     /**
  323.      * Constructor using an array of integers as seed. Your array must have a
  324.      * non-zero length. Only the first 624 integers in the array are used; if
  325.      * the array is shorter than this then integers are repeatedly used in a
  326.      * wrap-around fashion.
  327.      */
  328.     public MersenneTwisterFast(final int[] array) {
  329.         setSeed(array);
  330.     }
  331.  
  332.     /**
  333.      * Initalize the pseudo random number generator. Don't pass in a long that's
  334.      * bigger than an int (Mersenne Twister only uses the first 32 bits for its
  335.      * seed).
  336.      */
  337.  
  338.     synchronized public void setSeed(final long seed) {
  339.         // Due to a bug in java.util.Random clear up to 1.2, we're
  340.         // doing our own Gaussian variable.
  341.         __haveNextNextGaussian = false;
  342.  
  343.         mt = new int[N];
  344.  
  345.         mag01 = new int[2];
  346.         mag01[0] = 0x0;
  347.         mag01[1] = MATRIX_A;
  348.  
  349.         mt[0] = (int) (seed & 0xffffffff);
  350.         for (mti = 1; mti < N; mti++) {
  351.             mt[mti] = (1812433253 * (mt[mti - 1] ^ (mt[mti - 1] >>> 30)) + mti);
  352.             /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  353.             /* In the previous versions, MSBs of the seed affect */
  354.             /* only MSBs of the array mt[]. */
  355.             /* 2002/01/09 modified by Makoto Matsumoto */
  356.             // mt[mti] &= 0xffffffff;
  357.             /* for >32 bit machines */
  358.         }
  359.     }
  360.  
  361.     /**
  362.      * Sets the seed of the MersenneTwister using an array of integers. Your
  363.      * array must have a non-zero length. Only the first 624 integers in the
  364.      * array are used; if the array is shorter than this then integers are
  365.      * repeatedly used in a wrap-around fashion.
  366.      */
  367.  
  368.     synchronized public void setSeed(final int[] array) {
  369.         if (array.length == 0) {
  370.             throw new IllegalArgumentException("Array length must be greater than zero");
  371.         }
  372.         int i, j, k;
  373.         setSeed(19650218);
  374.         i = 1;
  375.         j = 0;
  376.         k = (N > array.length ? N : array.length);
  377.         for (; k != 0; k--) {
  378.             mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1664525)) + array[j] + j; /*
  379.                                                                                              * non
  380.                                                                                              * linear
  381.                                                                                              */
  382.             // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  383.             i++;
  384.             j++;
  385.             if (i >= N) {
  386.                 mt[0] = mt[N - 1];
  387.                 i = 1;
  388.             }
  389.             if (j >= array.length) {
  390.                 j = 0;
  391.             }
  392.         }
  393.         for (k = N - 1; k != 0; k--) {
  394.             mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >>> 30)) * 1566083941)) - i; /*
  395.                                                                                      * non
  396.                                                                                      * linear
  397.                                                                                      */
  398.             // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  399.             i++;
  400.             if (i >= N) {
  401.                 mt[0] = mt[N - 1];
  402.                 i = 1;
  403.             }
  404.         }
  405.         mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  406.     }
  407.  
  408.     public int nextInt() {
  409.         int y;
  410.  
  411.         if (mti >= N) // generate N words at one time
  412.         {
  413.             int kk;
  414.             final int[] mt = this.mt; // locals are slightly faster
  415.             final int[] mag01 = this.mag01; // locals are slightly faster
  416.  
  417.             for (kk = 0; kk < N - M; kk++) {
  418.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  419.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  420.             }
  421.             for (; kk < N - 1; kk++) {
  422.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  423.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  424.             }
  425.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  426.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  427.  
  428.             mti = 0;
  429.         }
  430.  
  431.         y = mt[mti++];
  432.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  433.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  434.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  435.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  436.  
  437.         return y;
  438.     }
  439.  
  440.     public short nextShort() {
  441.         int y;
  442.  
  443.         if (mti >= N) // generate N words at one time
  444.         {
  445.             int kk;
  446.             final int[] mt = this.mt; // locals are slightly faster
  447.             final int[] mag01 = this.mag01; // locals are slightly faster
  448.  
  449.             for (kk = 0; kk < N - M; kk++) {
  450.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  451.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  452.             }
  453.             for (; kk < N - 1; kk++) {
  454.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  455.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  456.             }
  457.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  458.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  459.  
  460.             mti = 0;
  461.         }
  462.  
  463.         y = mt[mti++];
  464.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  465.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  466.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  467.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  468.  
  469.         return (short) (y >>> 16);
  470.     }
  471.  
  472.     public char nextChar() {
  473.         int y;
  474.  
  475.         if (mti >= N) // generate N words at one time
  476.         {
  477.             int kk;
  478.             final int[] mt = this.mt; // locals are slightly faster
  479.             final int[] mag01 = this.mag01; // locals are slightly faster
  480.  
  481.             for (kk = 0; kk < N - M; kk++) {
  482.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  483.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  484.             }
  485.             for (; kk < N - 1; kk++) {
  486.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  487.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  488.             }
  489.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  490.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  491.  
  492.             mti = 0;
  493.         }
  494.  
  495.         y = mt[mti++];
  496.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  497.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  498.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  499.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  500.  
  501.         return (char) (y >>> 16);
  502.     }
  503.  
  504.     public boolean nextBoolean() {
  505.         int y;
  506.  
  507.         if (mti >= N) // generate N words at one time
  508.         {
  509.             int kk;
  510.             final int[] mt = this.mt; // locals are slightly faster
  511.             final int[] mag01 = this.mag01; // locals are slightly faster
  512.  
  513.             for (kk = 0; kk < N - M; kk++) {
  514.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  515.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  516.             }
  517.             for (; kk < N - 1; kk++) {
  518.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  519.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  520.             }
  521.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  522.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  523.  
  524.             mti = 0;
  525.         }
  526.  
  527.         y = mt[mti++];
  528.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  529.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  530.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  531.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  532.  
  533.         return (y >>> 31) != 0;
  534.     }
  535.  
  536.     /**
  537.      * This generates a coin flip with a probability <tt>probability</tt> of
  538.      * returning true, else returning false. <tt>probability</tt> must be
  539.      * between 0.0 and 1.0, inclusive. Not as precise a random real event as
  540.      * nextBoolean(double), but twice as fast. To explicitly use this, remember
  541.      * you may need to cast to float first.
  542.      */
  543.  
  544.     public boolean nextBoolean(final float probability) {
  545.         int y;
  546.  
  547.         if (probability < 0.0f || probability > 1.0f) {
  548.             throw new IllegalArgumentException("probability must be between 0.0 and 1.0 inclusive.");
  549.         }
  550.         if (probability == 0.0f) {
  551.             return false; // fix half-open issues
  552.         } else if (probability == 1.0f) {
  553.             return true; // fix half-open issues
  554.         }
  555.         if (mti >= N) // generate N words at one time
  556.         {
  557.             int kk;
  558.             final int[] mt = this.mt; // locals are slightly faster
  559.             final int[] mag01 = this.mag01; // locals are slightly faster
  560.  
  561.             for (kk = 0; kk < N - M; kk++) {
  562.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  563.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  564.             }
  565.             for (; kk < N - 1; kk++) {
  566.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  567.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  568.             }
  569.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  570.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  571.  
  572.             mti = 0;
  573.         }
  574.  
  575.         y = mt[mti++];
  576.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  577.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  578.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  579.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  580.  
  581.         return (y >>> 8) / ((float) (1 << 24)) < probability;
  582.     }
  583.  
  584.     /**
  585.      * This generates a coin flip with a probability <tt>probability</tt> of
  586.      * returning true, else returning false. <tt>probability</tt> must be
  587.      * between 0.0 and 1.0, inclusive.
  588.      */
  589.  
  590.     public boolean nextBoolean(final double probability) {
  591.         int y;
  592.         int z;
  593.  
  594.         if (probability < 0.0 || probability > 1.0) {
  595.             throw new IllegalArgumentException("probability must be between 0.0 and 1.0 inclusive.");
  596.         }
  597.         if (probability == 0.0) {
  598.             return false; // fix half-open issues
  599.         } else if (probability == 1.0) {
  600.             return true; // fix half-open issues
  601.         }
  602.         if (mti >= N) // generate N words at one time
  603.         {
  604.             int kk;
  605.             final int[] mt = this.mt; // locals are slightly faster
  606.             final int[] mag01 = this.mag01; // locals are slightly faster
  607.  
  608.             for (kk = 0; kk < N - M; kk++) {
  609.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  610.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  611.             }
  612.             for (; kk < N - 1; kk++) {
  613.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  614.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  615.             }
  616.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  617.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  618.  
  619.             mti = 0;
  620.         }
  621.  
  622.         y = mt[mti++];
  623.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  624.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  625.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  626.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  627.  
  628.         if (mti >= N) // generate N words at one time
  629.         {
  630.             int kk;
  631.             final int[] mt = this.mt; // locals are slightly faster
  632.             final int[] mag01 = this.mag01; // locals are slightly faster
  633.  
  634.             for (kk = 0; kk < N - M; kk++) {
  635.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  636.                 mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1];
  637.             }
  638.             for (; kk < N - 1; kk++) {
  639.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  640.                 mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1];
  641.             }
  642.             z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  643.             mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1];
  644.  
  645.             mti = 0;
  646.         }
  647.  
  648.         z = mt[mti++];
  649.         z ^= z >>> 11; // TEMPERING_SHIFT_U(z)
  650.         z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z)
  651.         z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z)
  652.         z ^= (z >>> 18); // TEMPERING_SHIFT_L(z)
  653.  
  654.         /* derived from nextDouble documentation in jdk 1.2 docs, see top */
  655.         return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53) < probability;
  656.     }
  657.  
  658.     public byte nextByte() {
  659.         int y;
  660.  
  661.         if (mti >= N) // generate N words at one time
  662.         {
  663.             int kk;
  664.             final int[] mt = this.mt; // locals are slightly faster
  665.             final int[] mag01 = this.mag01; // locals are slightly faster
  666.  
  667.             for (kk = 0; kk < N - M; kk++) {
  668.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  669.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  670.             }
  671.             for (; kk < N - 1; kk++) {
  672.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  673.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  674.             }
  675.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  676.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  677.  
  678.             mti = 0;
  679.         }
  680.  
  681.         y = mt[mti++];
  682.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  683.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  684.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  685.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  686.  
  687.         return (byte) (y >>> 24);
  688.     }
  689.  
  690.     public void nextBytes(final byte[] bytes) {
  691.         int y;
  692.  
  693.         for (int x = 0; x < bytes.length; x++) {
  694.             if (mti >= N) // generate N words at one time
  695.             {
  696.                 int kk;
  697.                 final int[] mt = this.mt; // locals are slightly faster
  698.                 final int[] mag01 = this.mag01; // locals are slightly faster
  699.  
  700.                 for (kk = 0; kk < N - M; kk++) {
  701.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  702.                     mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  703.                 }
  704.                 for (; kk < N - 1; kk++) {
  705.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  706.                     mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  707.                 }
  708.                 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  709.                 mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  710.  
  711.                 mti = 0;
  712.             }
  713.  
  714.             y = mt[mti++];
  715.             y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  716.             y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  717.             y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  718.             y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  719.  
  720.             bytes[x] = (byte) (y >>> 24);
  721.         }
  722.     }
  723.  
  724.     public long nextLong() {
  725.         int y;
  726.         int z;
  727.  
  728.         if (mti >= N) // generate N words at one time
  729.         {
  730.             int kk;
  731.             final int[] mt = this.mt; // locals are slightly faster
  732.             final int[] mag01 = this.mag01; // locals are slightly faster
  733.  
  734.             for (kk = 0; kk < N - M; kk++) {
  735.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  736.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  737.             }
  738.             for (; kk < N - 1; kk++) {
  739.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  740.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  741.             }
  742.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  743.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  744.  
  745.             mti = 0;
  746.         }
  747.  
  748.         y = mt[mti++];
  749.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  750.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  751.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  752.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  753.  
  754.         if (mti >= N) // generate N words at one time
  755.         {
  756.             int kk;
  757.             final int[] mt = this.mt; // locals are slightly faster
  758.             final int[] mag01 = this.mag01; // locals are slightly faster
  759.  
  760.             for (kk = 0; kk < N - M; kk++) {
  761.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  762.                 mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1];
  763.             }
  764.             for (; kk < N - 1; kk++) {
  765.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  766.                 mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1];
  767.             }
  768.             z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  769.             mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1];
  770.  
  771.             mti = 0;
  772.         }
  773.  
  774.         z = mt[mti++];
  775.         z ^= z >>> 11; // TEMPERING_SHIFT_U(z)
  776.         z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z)
  777.         z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z)
  778.         z ^= (z >>> 18); // TEMPERING_SHIFT_L(z)
  779.  
  780.         return (((long) y) << 32) + z;
  781.     }
  782.  
  783.     /**
  784.      * Returns a long drawn uniformly from 0 to n-1. Suffice it to say, n must
  785.      * be > 0, or an IllegalArgumentException is raised.
  786.      */
  787.     public long nextLong(final long n) {
  788.         if (n <= 0) {
  789.             throw new IllegalArgumentException("n must be positive, got: " + n);
  790.         }
  791.  
  792.         long bits, val;
  793.         do {
  794.             int y;
  795.             int z;
  796.  
  797.             if (mti >= N) // generate N words at one time
  798.             {
  799.                 int kk;
  800.                 final int[] mt = this.mt; // locals are slightly faster
  801.                 final int[] mag01 = this.mag01; // locals are slightly faster
  802.  
  803.                 for (kk = 0; kk < N - M; kk++) {
  804.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  805.                     mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  806.                 }
  807.                 for (; kk < N - 1; kk++) {
  808.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  809.                     mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  810.                 }
  811.                 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  812.                 mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  813.  
  814.                 mti = 0;
  815.             }
  816.  
  817.             y = mt[mti++];
  818.             y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  819.             y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  820.             y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  821.             y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  822.  
  823.             if (mti >= N) // generate N words at one time
  824.             {
  825.                 int kk;
  826.                 final int[] mt = this.mt; // locals are slightly faster
  827.                 final int[] mag01 = this.mag01; // locals are slightly faster
  828.  
  829.                 for (kk = 0; kk < N - M; kk++) {
  830.                     z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  831.                     mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1];
  832.                 }
  833.                 for (; kk < N - 1; kk++) {
  834.                     z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  835.                     mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1];
  836.                 }
  837.                 z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  838.                 mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1];
  839.  
  840.                 mti = 0;
  841.             }
  842.  
  843.             z = mt[mti++];
  844.             z ^= z >>> 11; // TEMPERING_SHIFT_U(z)
  845.             z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z)
  846.             z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z)
  847.             z ^= (z >>> 18); // TEMPERING_SHIFT_L(z)
  848.  
  849.             bits = (((((long) y) << 32) + z) >>> 1);
  850.             val = bits % n;
  851.         } while (bits - val + (n - 1) < 0);
  852.         return val;
  853.     }
  854.  
  855.     /**
  856.      * Returns a random double in the half-open range from [0.0,1.0). Thus 0.0
  857.      * is a valid result but 1.0 is not.
  858.      */
  859.     public double nextDouble() {
  860.         int y;
  861.         int z;
  862.  
  863.         if (mti >= N) // generate N words at one time
  864.         {
  865.             int kk;
  866.             final int[] mt = this.mt; // locals are slightly faster
  867.             final int[] mag01 = this.mag01; // locals are slightly faster
  868.  
  869.             for (kk = 0; kk < N - M; kk++) {
  870.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  871.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  872.             }
  873.             for (; kk < N - 1; kk++) {
  874.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  875.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  876.             }
  877.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  878.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  879.  
  880.             mti = 0;
  881.         }
  882.  
  883.         y = mt[mti++];
  884.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  885.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  886.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  887.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  888.  
  889.         if (mti >= N) // generate N words at one time
  890.         {
  891.             int kk;
  892.             final int[] mt = this.mt; // locals are slightly faster
  893.             final int[] mag01 = this.mag01; // locals are slightly faster
  894.  
  895.             for (kk = 0; kk < N - M; kk++) {
  896.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  897.                 mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1];
  898.             }
  899.             for (; kk < N - 1; kk++) {
  900.                 z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  901.                 mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1];
  902.             }
  903.             z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  904.             mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1];
  905.  
  906.             mti = 0;
  907.         }
  908.  
  909.         z = mt[mti++];
  910.         z ^= z >>> 11; // TEMPERING_SHIFT_U(z)
  911.         z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z)
  912.         z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z)
  913.         z ^= (z >>> 18); // TEMPERING_SHIFT_L(z)
  914.  
  915.         /* derived from nextDouble documentation in jdk 1.2 docs, see top */
  916.         return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53);
  917.     }
  918.  
  919.     /**
  920.      * Returns a double in the range from 0.0 to 1.0, possibly inclusive of 0.0
  921.      * and 1.0 themselves. Thus:
  922.      *
  923.      * <p>
  924.      * <table border=0>
  925.      * <th>
  926.      * <td>Expression
  927.      * <td>Interval
  928.      * <tr>
  929.      * <td>nextDouble(false, false)
  930.      * <td>(0.0, 1.0)
  931.      * <tr>
  932.      * <td>nextDouble(true, false)
  933.      * <td>[0.0, 1.0)
  934.      * <tr>
  935.      * <td>nextDouble(false, true)
  936.      * <td>(0.0, 1.0]
  937.      * <tr>
  938.      * <td>nextDouble(true, true)
  939.      * <td>[0.0, 1.0]
  940.      * </table>
  941.      *
  942.      * <p>
  943.      * This version preserves all possible random values in the double range.
  944.      */
  945.     public double nextDouble(final boolean includeZero, final boolean includeOne) {
  946.         double d = 0.0;
  947.         do {
  948.             d = nextDouble(); // grab a value, initially from half-open [0.0,
  949.                                 // 1.0)
  950.             if (includeOne && nextBoolean()) {
  951.                 d += 1.0; // if includeOne, with 1/2 probability, push to [1.0,
  952.                             // 2.0)
  953.             }
  954.         } while ((d > 1.0) || // everything above 1.0 is always invalid
  955.                 (!includeZero && d == 0.0)); // if we're not including zero, 0.0
  956.                                                 // is invalid
  957.         return d;
  958.     }
  959.  
  960.     public double nextGaussian() {
  961.         if (__haveNextNextGaussian) {
  962.             __haveNextNextGaussian = false;
  963.             return __nextNextGaussian;
  964.         } else {
  965.             double v1, v2, s;
  966.             do {
  967.                 int y;
  968.                 int z;
  969.                 int a;
  970.                 int b;
  971.  
  972.                 if (mti >= N) // generate N words at one time
  973.                 {
  974.                     int kk;
  975.                     final int[] mt = this.mt; // locals are slightly faster
  976.                     final int[] mag01 = this.mag01; // locals are slightly
  977.                                                     // faster
  978.  
  979.                     for (kk = 0; kk < N - M; kk++) {
  980.                         y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  981.                         mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  982.                     }
  983.                     for (; kk < N - 1; kk++) {
  984.                         y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  985.                         mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  986.                     }
  987.                     y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  988.                     mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  989.  
  990.                     mti = 0;
  991.                 }
  992.  
  993.                 y = mt[mti++];
  994.                 y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  995.                 y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  996.                 y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  997.                 y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  998.  
  999.                 if (mti >= N) // generate N words at one time
  1000.                 {
  1001.                     int kk;
  1002.                     final int[] mt = this.mt; // locals are slightly faster
  1003.                     final int[] mag01 = this.mag01; // locals are slightly
  1004.                                                     // faster
  1005.  
  1006.                     for (kk = 0; kk < N - M; kk++) {
  1007.                         z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1008.                         mt[kk] = mt[kk + M] ^ (z >>> 1) ^ mag01[z & 0x1];
  1009.                     }
  1010.                     for (; kk < N - 1; kk++) {
  1011.                         z = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1012.                         mt[kk] = mt[kk + (M - N)] ^ (z >>> 1) ^ mag01[z & 0x1];
  1013.                     }
  1014.                     z = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1015.                     mt[N - 1] = mt[M - 1] ^ (z >>> 1) ^ mag01[z & 0x1];
  1016.  
  1017.                     mti = 0;
  1018.                 }
  1019.  
  1020.                 z = mt[mti++];
  1021.                 z ^= z >>> 11; // TEMPERING_SHIFT_U(z)
  1022.                 z ^= (z << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(z)
  1023.                 z ^= (z << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(z)
  1024.                 z ^= (z >>> 18); // TEMPERING_SHIFT_L(z)
  1025.  
  1026.                 if (mti >= N) // generate N words at one time
  1027.                 {
  1028.                     int kk;
  1029.                     final int[] mt = this.mt; // locals are slightly faster
  1030.                     final int[] mag01 = this.mag01; // locals are slightly
  1031.                                                     // faster
  1032.  
  1033.                     for (kk = 0; kk < N - M; kk++) {
  1034.                         a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1035.                         mt[kk] = mt[kk + M] ^ (a >>> 1) ^ mag01[a & 0x1];
  1036.                     }
  1037.                     for (; kk < N - 1; kk++) {
  1038.                         a = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1039.                         mt[kk] = mt[kk + (M - N)] ^ (a >>> 1) ^ mag01[a & 0x1];
  1040.                     }
  1041.                     a = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1042.                     mt[N - 1] = mt[M - 1] ^ (a >>> 1) ^ mag01[a & 0x1];
  1043.  
  1044.                     mti = 0;
  1045.                 }
  1046.  
  1047.                 a = mt[mti++];
  1048.                 a ^= a >>> 11; // TEMPERING_SHIFT_U(a)
  1049.                 a ^= (a << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(a)
  1050.                 a ^= (a << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(a)
  1051.                 a ^= (a >>> 18); // TEMPERING_SHIFT_L(a)
  1052.  
  1053.                 if (mti >= N) // generate N words at one time
  1054.                 {
  1055.                     int kk;
  1056.                     final int[] mt = this.mt; // locals are slightly faster
  1057.                     final int[] mag01 = this.mag01; // locals are slightly
  1058.                                                     // faster
  1059.  
  1060.                     for (kk = 0; kk < N - M; kk++) {
  1061.                         b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1062.                         mt[kk] = mt[kk + M] ^ (b >>> 1) ^ mag01[b & 0x1];
  1063.                     }
  1064.                     for (; kk < N - 1; kk++) {
  1065.                         b = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1066.                         mt[kk] = mt[kk + (M - N)] ^ (b >>> 1) ^ mag01[b & 0x1];
  1067.                     }
  1068.                     b = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1069.                     mt[N - 1] = mt[M - 1] ^ (b >>> 1) ^ mag01[b & 0x1];
  1070.  
  1071.                     mti = 0;
  1072.                 }
  1073.  
  1074.                 b = mt[mti++];
  1075.                 b ^= b >>> 11; // TEMPERING_SHIFT_U(b)
  1076.                 b ^= (b << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(b)
  1077.                 b ^= (b << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(b)
  1078.                 b ^= (b >>> 18); // TEMPERING_SHIFT_L(b)
  1079.  
  1080.                 /*
  1081.                  * derived from nextDouble documentation in jdk 1.2 docs, see
  1082.                  * top
  1083.                  */
  1084.                 v1 = 2 * (((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53)) - 1;
  1085.                 v2 = 2 * (((((long) (a >>> 6)) << 27) + (b >>> 5)) / (double) (1L << 53)) - 1;
  1086.                 s = v1 * v1 + v2 * v2;
  1087.             } while (s >= 1 || s == 0);
  1088.             final double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s) / s);
  1089.             __nextNextGaussian = v2 * multiplier;
  1090.             __haveNextNextGaussian = true;
  1091.             return v1 * multiplier;
  1092.         }
  1093.     }
  1094.  
  1095.     /**
  1096.      * Returns a random float in the half-open range from [0.0f,1.0f). Thus 0.0f
  1097.      * is a valid result but 1.0f is not.
  1098.      */
  1099.     public float nextFloat() {
  1100.         int y;
  1101.  
  1102.         if (mti >= N) // generate N words at one time
  1103.         {
  1104.             int kk;
  1105.             final int[] mt = this.mt; // locals are slightly faster
  1106.             final int[] mag01 = this.mag01; // locals are slightly faster
  1107.  
  1108.             for (kk = 0; kk < N - M; kk++) {
  1109.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1110.                 mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  1111.             }
  1112.             for (; kk < N - 1; kk++) {
  1113.                 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1114.                 mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  1115.             }
  1116.             y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1117.             mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  1118.  
  1119.             mti = 0;
  1120.         }
  1121.  
  1122.         y = mt[mti++];
  1123.         y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  1124.         y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  1125.         y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  1126.         y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  1127.  
  1128.         return (y >>> 8) / ((float) (1 << 24));
  1129.     }
  1130.  
  1131.     /**
  1132.      * Returns a float in the range from 0.0f to 1.0f, possibly inclusive of
  1133.      * 0.0f and 1.0f themselves. Thus:
  1134.      *
  1135.      * <p>
  1136.      * <table border=0>
  1137.      * <th>
  1138.      * <td>Expression
  1139.      * <td>Interval
  1140.      * <tr>
  1141.      * <td>nextFloat(false, false)
  1142.      * <td>(0.0f, 1.0f)
  1143.      * <tr>
  1144.      * <td>nextFloat(true, false)
  1145.      * <td>[0.0f, 1.0f)
  1146.      * <tr>
  1147.      * <td>nextFloat(false, true)
  1148.      * <td>(0.0f, 1.0f]
  1149.      * <tr>
  1150.      * <td>nextFloat(true, true)
  1151.      * <td>[0.0f, 1.0f]
  1152.      * </table>
  1153.      *
  1154.      * <p>
  1155.      * This version preserves all possible random values in the float range.
  1156.      */
  1157.     public float nextFloat(final boolean includeZero, final boolean includeOne) {
  1158.         float d = 0.0f;
  1159.         do {
  1160.             d = nextFloat(); // grab a value, initially from half-open [0.0f,
  1161.                                 // 1.0f)
  1162.             if (includeOne && nextBoolean()) {
  1163.                 d += 1.0f; // if includeOne, with 1/2 probability, push to
  1164.                             // [1.0f, 2.0f)
  1165.             }
  1166.         } while ((d > 1.0f) || // everything above 1.0f is always invalid
  1167.                 (!includeZero && d == 0.0f)); // if we're not including zero,
  1168.                                                 // 0.0f is invalid
  1169.         return d;
  1170.     }
  1171.  
  1172.     /**
  1173.      * Returns an integer drawn uniformly from 0 to n-1. Suffice it to say, n
  1174.      * must be > 0, or an IllegalArgumentException is raised.
  1175.      */
  1176.     public int nextInt(final int n) {
  1177.         if (n <= 0) {
  1178.             throw new IllegalArgumentException("n must be positive, got: " + n);
  1179.         }
  1180.  
  1181.         if ((n & -n) == n) // i.e., n is a power of 2
  1182.         {
  1183.             int y;
  1184.  
  1185.             if (mti >= N) // generate N words at one time
  1186.             {
  1187.                 int kk;
  1188.                 final int[] mt = this.mt; // locals are slightly faster
  1189.                 final int[] mag01 = this.mag01; // locals are slightly faster
  1190.  
  1191.                 for (kk = 0; kk < N - M; kk++) {
  1192.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1193.                     mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  1194.                 }
  1195.                 for (; kk < N - 1; kk++) {
  1196.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1197.                     mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  1198.                 }
  1199.                 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1200.                 mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  1201.  
  1202.                 mti = 0;
  1203.             }
  1204.  
  1205.             y = mt[mti++];
  1206.             y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  1207.             y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  1208.             y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  1209.             y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  1210.  
  1211.             return (int) ((n * (long) (y >>> 1)) >> 31);
  1212.         }
  1213.  
  1214.         int bits, val;
  1215.         do {
  1216.             int y;
  1217.  
  1218.             if (mti >= N) // generate N words at one time
  1219.             {
  1220.                 int kk;
  1221.                 final int[] mt = this.mt; // locals are slightly faster
  1222.                 final int[] mag01 = this.mag01; // locals are slightly faster
  1223.  
  1224.                 for (kk = 0; kk < N - M; kk++) {
  1225.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1226.                     mt[kk] = mt[kk + M] ^ (y >>> 1) ^ mag01[y & 0x1];
  1227.                 }
  1228.                 for (; kk < N - 1; kk++) {
  1229.                     y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
  1230.                     mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  1231.                 }
  1232.                 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  1233.                 mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
  1234.  
  1235.                 mti = 0;
  1236.             }
  1237.  
  1238.             y = mt[mti++];
  1239.             y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  1240.             y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  1241.             y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  1242.             y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  1243.  
  1244.             bits = (y >>> 1);
  1245.             val = bits % n;
  1246.         } while (bits - val + (n - 1) < 0);
  1247.         return val;
  1248.     }
  1249.  
  1250.     /**
  1251.      * Tests the code.
  1252.      */
  1253.     public static void main(final String args[]) {
  1254.         int j;
  1255.  
  1256.         MersenneTwisterFast r;
  1257.  
  1258.         // CORRECTNESS TEST
  1259.         // COMPARE WITH
  1260.         // http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out
  1261.  
  1262.         r = new MersenneTwisterFast(new int[] { 0x123, 0x234, 0x345, 0x456 });
  1263.         System.out.println("Output of MersenneTwisterFast with new (2002/1/26) seeding mechanism");
  1264.         for (j = 0; j < 1000; j++) {
  1265.             // first, convert the int from signed to "unsigned"
  1266.             long l = r.nextInt();
  1267.             if (l < 0) {
  1268.                 l += 4294967296L; // max int value
  1269.             }
  1270.             String s = String.valueOf(l);
  1271.             while (s.length() < 10) {
  1272.                 s = " " + s; // buffer
  1273.             }
  1274.             System.out.print(s + " ");
  1275.             if (j % 5 == 4) {
  1276.                 System.out.println();
  1277.             }
  1278.         }
  1279.  
  1280.         // SPEED TEST
  1281.  
  1282.         final long SEED = 4357;
  1283.  
  1284.         int xx;
  1285.         long ms;
  1286.         System.out.println("\nTime to test grabbing 100000000 ints");
  1287.  
  1288.         final Random rr = new Random(SEED);
  1289.         xx = 0;
  1290.         ms = System.currentTimeMillis();
  1291.         for (j = 0; j < 100000000; j++) {
  1292.             xx += rr.nextInt();
  1293.         }
  1294.         System.out.println("java.util.Random: " + (System.currentTimeMillis() - ms) + "          Ignore this: " + xx);
  1295.  
  1296.         r = new MersenneTwisterFast(SEED);
  1297.         ms = System.currentTimeMillis();
  1298.         xx = 0;
  1299.         for (j = 0; j < 100000000; j++) {
  1300.             xx += r.nextInt();
  1301.         }
  1302.         System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + "          Ignore this: " + xx);
  1303.  
  1304.         // TEST TO COMPARE TYPE CONVERSION BETWEEN
  1305.         // MersenneTwisterFast.java AND MersenneTwister.java
  1306.  
  1307.         System.out.println("\nGrab the first 1000 booleans");
  1308.         r = new MersenneTwisterFast(SEED);
  1309.         for (j = 0; j < 1000; j++) {
  1310.             System.out.print(r.nextBoolean() + " ");
  1311.             if (j % 8 == 7) {
  1312.                 System.out.println();
  1313.             }
  1314.         }
  1315.         if (!(j % 8 == 7)) {
  1316.             System.out.println();
  1317.         }
  1318.  
  1319.         System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(double)");
  1320.         r = new MersenneTwisterFast(SEED);
  1321.         for (j = 0; j < 1000; j++) {
  1322.             System.out.print(r.nextBoolean(j / 999.0) + " ");
  1323.             if (j % 8 == 7) {
  1324.                 System.out.println();
  1325.             }
  1326.         }
  1327.         if (!(j % 8 == 7)) {
  1328.             System.out.println();
  1329.         }
  1330.  
  1331.         System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(float)");
  1332.         r = new MersenneTwisterFast(SEED);
  1333.         for (j = 0; j < 1000; j++) {
  1334.             System.out.print(r.nextBoolean(j / 999.0f) + " ");
  1335.             if (j % 8 == 7) {
  1336.                 System.out.println();
  1337.             }
  1338.         }
  1339.         if (!(j % 8 == 7)) {
  1340.             System.out.println();
  1341.         }
  1342.  
  1343.         final byte[] bytes = new byte[1000];
  1344.         System.out.println("\nGrab the first 1000 bytes using nextBytes");
  1345.         r = new MersenneTwisterFast(SEED);
  1346.         r.nextBytes(bytes);
  1347.         for (j = 0; j < 1000; j++) {
  1348.             System.out.print(bytes[j] + " ");
  1349.             if (j % 16 == 15) {
  1350.                 System.out.println();
  1351.             }
  1352.         }
  1353.         if (!(j % 16 == 15)) {
  1354.             System.out.println();
  1355.         }
  1356.  
  1357.         byte b;
  1358.         System.out.println("\nGrab the first 1000 bytes -- must be same as nextBytes");
  1359.         r = new MersenneTwisterFast(SEED);
  1360.         for (j = 0; j < 1000; j++) {
  1361.             System.out.print((b = r.nextByte()) + " ");
  1362.             if (b != bytes[j]) {
  1363.                 System.out.print("BAD ");
  1364.             }
  1365.             if (j % 16 == 15) {
  1366.                 System.out.println();
  1367.             }
  1368.         }
  1369.         if (!(j % 16 == 15)) {
  1370.             System.out.println();
  1371.         }
  1372.  
  1373.         System.out.println("\nGrab the first 1000 shorts");
  1374.         r = new MersenneTwisterFast(SEED);
  1375.         for (j = 0; j < 1000; j++) {
  1376.             System.out.print(r.nextShort() + " ");
  1377.             if (j % 8 == 7) {
  1378.                 System.out.println();
  1379.             }
  1380.         }
  1381.         if (!(j % 8 == 7)) {
  1382.             System.out.println();
  1383.         }
  1384.  
  1385.         System.out.println("\nGrab the first 1000 ints");
  1386.         r = new MersenneTwisterFast(SEED);
  1387.         for (j = 0; j < 1000; j++) {
  1388.             System.out.print(r.nextInt() + " ");
  1389.             if (j % 4 == 3) {
  1390.                 System.out.println();
  1391.             }
  1392.         }
  1393.         if (!(j % 4 == 3)) {
  1394.             System.out.println();
  1395.         }
  1396.  
  1397.         System.out.println("\nGrab the first 1000 ints of different sizes");
  1398.         r = new MersenneTwisterFast(SEED);
  1399.         int max = 1;
  1400.         for (j = 0; j < 1000; j++) {
  1401.             System.out.print(r.nextInt(max) + " ");
  1402.             max *= 2;
  1403.             if (max <= 0) {
  1404.                 max = 1;
  1405.             }
  1406.             if (j % 4 == 3) {
  1407.                 System.out.println();
  1408.             }
  1409.         }
  1410.         if (!(j % 4 == 3)) {
  1411.             System.out.println();
  1412.         }
  1413.  
  1414.         System.out.println("\nGrab the first 1000 longs");
  1415.         r = new MersenneTwisterFast(SEED);
  1416.         for (j = 0; j < 1000; j++) {
  1417.             System.out.print(r.nextLong() + " ");
  1418.             if (j % 3 == 2) {
  1419.                 System.out.println();
  1420.             }
  1421.         }
  1422.         if (!(j % 3 == 2)) {
  1423.             System.out.println();
  1424.         }
  1425.  
  1426.         System.out.println("\nGrab the first 1000 longs of different sizes");
  1427.         r = new MersenneTwisterFast(SEED);
  1428.         long max2 = 1;
  1429.         for (j = 0; j < 1000; j++) {
  1430.             System.out.print(r.nextLong(max2) + " ");
  1431.             max2 *= 2;
  1432.             if (max2 <= 0) {
  1433.                 max2 = 1;
  1434.             }
  1435.             if (j % 4 == 3) {
  1436.                 System.out.println();
  1437.             }
  1438.         }
  1439.         if (!(j % 4 == 3)) {
  1440.             System.out.println();
  1441.         }
  1442.  
  1443.         System.out.println("\nGrab the first 1000 floats");
  1444.         r = new MersenneTwisterFast(SEED);
  1445.         for (j = 0; j < 1000; j++) {
  1446.             System.out.print(r.nextFloat() + " ");
  1447.             if (j % 4 == 3) {
  1448.                 System.out.println();
  1449.             }
  1450.         }
  1451.         if (!(j % 4 == 3)) {
  1452.             System.out.println();
  1453.         }
  1454.  
  1455.         System.out.println("\nGrab the first 1000 doubles");
  1456.         r = new MersenneTwisterFast(SEED);
  1457.         for (j = 0; j < 1000; j++) {
  1458.             System.out.print(r.nextDouble() + " ");
  1459.             if (j % 3 == 2) {
  1460.                 System.out.println();
  1461.             }
  1462.         }
  1463.         if (!(j % 3 == 2)) {
  1464.             System.out.println();
  1465.         }
  1466.  
  1467.         System.out.println("\nGrab the first 1000 gaussian doubles");
  1468.         r = new MersenneTwisterFast(SEED);
  1469.         for (j = 0; j < 1000; j++) {
  1470.             System.out.print(r.nextGaussian() + " ");
  1471.             if (j % 3 == 2) {
  1472.                 System.out.println();
  1473.             }
  1474.         }
  1475.         if (!(j % 3 == 2)) {
  1476.             System.out.println();
  1477.         }
  1478.  
  1479.     }
  1480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement