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