[6152] | 1 | package ec.util; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 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 | public strictfp class MersenneTwister extends java.util.Random implements Serializable, Cloneable |
---|
| 157 | { |
---|
| 158 | // Serialization |
---|
| 159 | private static final long serialVersionUID = -4035832775130174188L; // locked as of Version 15 |
---|
| 160 | |
---|
| 161 | // Period parameters |
---|
| 162 | private static final int N = 624; |
---|
| 163 | private static final int M = 397; |
---|
| 164 | private static final int MATRIX_A = 0x9908b0df; // private static final * constant vector a |
---|
| 165 | private static final int UPPER_MASK = 0x80000000; // most significant w-r bits |
---|
| 166 | private static final int LOWER_MASK = 0x7fffffff; // least significant r bits |
---|
| 167 | |
---|
| 168 | // Tempering parameters |
---|
| 169 | private static final int TEMPERING_MASK_B = 0x9d2c5680; |
---|
| 170 | private static final int TEMPERING_MASK_C = 0xefc60000; |
---|
| 171 | |
---|
| 172 | private int mt[]; // the array for the state vector |
---|
| 173 | private int mti; // mti==N+1 means mt[N] is not initialized |
---|
| 174 | private int mag01[]; |
---|
| 175 | |
---|
| 176 | // a good initial seed (of int size, though stored in a long) |
---|
| 177 | //private static final long GOOD_SEED = 4357; |
---|
| 178 | |
---|
| 179 | /* implemented here because there's a bug in Random's implementation |
---|
| 180 | of the Gaussian code (divide by zero, and log(0), ugh!), yet its |
---|
| 181 | gaussian variables are private so we can't access them here. :-( */ |
---|
| 182 | |
---|
| 183 | private double __nextNextGaussian; |
---|
| 184 | private boolean __haveNextNextGaussian; |
---|
| 185 | |
---|
| 186 | /* We're overriding all internal data, to my knowledge, so this should be okay */ |
---|
| 187 | public Object clone() |
---|
| 188 | { |
---|
| 189 | try |
---|
| 190 | { |
---|
| 191 | MersenneTwister f = (MersenneTwister)(super.clone()); |
---|
| 192 | f.mt = (int[])(mt.clone()); |
---|
| 193 | f.mag01 = (int[])(mag01.clone()); |
---|
| 194 | return f; |
---|
| 195 | } |
---|
| 196 | catch (CloneNotSupportedException e) { throw new InternalError(); } // should never happen |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | public boolean stateEquals(Object o) |
---|
| 200 | { |
---|
| 201 | if (o==this) return true; |
---|
| 202 | if (o == null || !(o instanceof MersenneTwister)) |
---|
| 203 | return false; |
---|
| 204 | MersenneTwister other = (MersenneTwister) o; |
---|
| 205 | if (mti != other.mti) return false; |
---|
| 206 | for(int x=0;x<mag01.length;x++) |
---|
| 207 | if (mag01[x] != other.mag01[x]) return false; |
---|
| 208 | for(int x=0;x<mt.length;x++) |
---|
| 209 | if (mt[x] != other.mt[x]) return false; |
---|
| 210 | return true; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** Reads the entire state of the MersenneTwister RNG from the stream */ |
---|
| 214 | public void readState(DataInputStream stream) throws IOException |
---|
| 215 | { |
---|
| 216 | int len = mt.length; |
---|
| 217 | for(int x=0;x<len;x++) mt[x] = stream.readInt(); |
---|
| 218 | |
---|
| 219 | len = mag01.length; |
---|
| 220 | for(int x=0;x<len;x++) mag01[x] = stream.readInt(); |
---|
| 221 | |
---|
| 222 | mti = stream.readInt(); |
---|
| 223 | __nextNextGaussian = stream.readDouble(); |
---|
| 224 | __haveNextNextGaussian = stream.readBoolean(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /** Writes the entire state of the MersenneTwister RNG to the stream */ |
---|
| 228 | public void writeState(DataOutputStream stream) throws IOException |
---|
| 229 | { |
---|
| 230 | int len = mt.length; |
---|
| 231 | for(int x=0;x<len;x++) stream.writeInt(mt[x]); |
---|
| 232 | |
---|
| 233 | len = mag01.length; |
---|
| 234 | for(int x=0;x<len;x++) stream.writeInt(mag01[x]); |
---|
| 235 | |
---|
| 236 | stream.writeInt(mti); |
---|
| 237 | stream.writeDouble(__nextNextGaussian); |
---|
| 238 | stream.writeBoolean(__haveNextNextGaussian); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * Constructor using the default seed. |
---|
| 244 | */ |
---|
| 245 | public MersenneTwister() |
---|
| 246 | { |
---|
| 247 | this(System.currentTimeMillis()); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Constructor using a given seed. Though you pass this seed in |
---|
| 252 | * as a long, it's best to make sure it's actually an integer. |
---|
| 253 | */ |
---|
| 254 | public MersenneTwister(final long seed) |
---|
| 255 | { |
---|
| 256 | super(seed); /* just in case */ |
---|
| 257 | setSeed(seed); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | * Constructor using an array of integers as seed. |
---|
| 262 | * Your array must have a non-zero length. Only the first 624 integers |
---|
| 263 | * in the array are used; if the array is shorter than this then |
---|
| 264 | * integers are repeatedly used in a wrap-around fashion. |
---|
| 265 | */ |
---|
| 266 | public MersenneTwister(final int[] array) |
---|
| 267 | { |
---|
| 268 | super(System.currentTimeMillis()); /* pick something at random just in case */ |
---|
| 269 | setSeed(array); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /** |
---|
| 273 | * Initalize the pseudo random number generator. Don't |
---|
| 274 | * pass in a long that's bigger than an int (Mersenne Twister |
---|
| 275 | * only uses the first 32 bits for its seed). |
---|
| 276 | */ |
---|
| 277 | |
---|
| 278 | synchronized public void setSeed(final long seed) |
---|
| 279 | { |
---|
| 280 | // it's always good style to call super |
---|
| 281 | super.setSeed(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 | |
---|
| 347 | /** |
---|
| 348 | * Returns an integer with <i>bits</i> bits filled with a random number. |
---|
| 349 | */ |
---|
| 350 | synchronized protected int next(final int bits) |
---|
| 351 | { |
---|
| 352 | int y; |
---|
| 353 | |
---|
| 354 | if (mti >= N) // generate N words at one time |
---|
| 355 | { |
---|
| 356 | int kk; |
---|
| 357 | final int[] mt = this.mt; // locals are slightly faster |
---|
| 358 | final int[] mag01 = this.mag01; // locals are slightly faster |
---|
| 359 | |
---|
| 360 | for (kk = 0; kk < N - M; kk++) |
---|
| 361 | { |
---|
| 362 | y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
---|
| 363 | mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; |
---|
| 364 | } |
---|
| 365 | for (; kk < N-1; kk++) |
---|
| 366 | { |
---|
| 367 | y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
---|
| 368 | mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; |
---|
| 369 | } |
---|
| 370 | y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
---|
| 371 | mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; |
---|
| 372 | |
---|
| 373 | mti = 0; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | y = mt[mti++]; |
---|
| 377 | y ^= y >>> 11; // TEMPERING_SHIFT_U(y) |
---|
| 378 | y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y) |
---|
| 379 | y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y) |
---|
| 380 | y ^= (y >>> 18); // TEMPERING_SHIFT_L(y) |
---|
| 381 | |
---|
| 382 | return y >>> (32 - bits); // hope that's right! |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /* If you've got a truly old version of Java, you can omit these |
---|
| 386 | two next methods. */ |
---|
| 387 | |
---|
| 388 | private synchronized void writeObject(final ObjectOutputStream out) |
---|
| 389 | throws IOException |
---|
| 390 | { |
---|
| 391 | // just so we're synchronized. |
---|
| 392 | out.defaultWriteObject(); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | private synchronized void readObject (final ObjectInputStream in) |
---|
| 396 | throws IOException, ClassNotFoundException |
---|
| 397 | { |
---|
| 398 | // just so we're synchronized. |
---|
| 399 | in.defaultReadObject(); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | /** This method is missing from jdk 1.0.x and below. JDK 1.1 |
---|
| 403 | includes this for us, but what the heck.*/ |
---|
| 404 | public boolean nextBoolean() {return next(1) != 0;} |
---|
| 405 | |
---|
| 406 | /** This generates a coin flip with a probability <tt>probability</tt> |
---|
| 407 | of returning true, else returning false. <tt>probability</tt> must |
---|
| 408 | be between 0.0 and 1.0, inclusive. Not as precise a random real |
---|
| 409 | event as nextBoolean(double), but twice as fast. To explicitly |
---|
| 410 | use this, remember you may need to cast to float first. */ |
---|
| 411 | |
---|
| 412 | public boolean nextBoolean (final float probability) |
---|
| 413 | { |
---|
| 414 | if (probability < 0.0f || probability > 1.0f) |
---|
| 415 | throw new IllegalArgumentException ("probability must be between 0.0 and 1.0 inclusive."); |
---|
| 416 | if (probability==0.0f) return false; // fix half-open issues |
---|
| 417 | else if (probability==1.0f) return true; // fix half-open issues |
---|
| 418 | return nextFloat() < probability; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | /** This generates a coin flip with a probability <tt>probability</tt> |
---|
| 422 | of returning true, else returning false. <tt>probability</tt> must |
---|
| 423 | be between 0.0 and 1.0, inclusive. */ |
---|
| 424 | |
---|
| 425 | public boolean nextBoolean (final double probability) |
---|
| 426 | { |
---|
| 427 | if (probability < 0.0 || probability > 1.0) |
---|
| 428 | throw new IllegalArgumentException ("probability must be between 0.0 and 1.0 inclusive."); |
---|
| 429 | if (probability==0.0) return false; // fix half-open issues |
---|
| 430 | else if (probability==1.0) return true; // fix half-open issues |
---|
| 431 | return nextDouble() < probability; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | /** This method is missing from JDK 1.1 and below. JDK 1.2 |
---|
| 435 | includes this for us, but what the heck. */ |
---|
| 436 | |
---|
| 437 | public int nextInt(final int n) |
---|
| 438 | { |
---|
| 439 | if (n<=0) |
---|
| 440 | throw new IllegalArgumentException("n must be positive, got: " + n); |
---|
| 441 | |
---|
| 442 | if ((n & -n) == n) |
---|
| 443 | return (int)((n * (long)next(31)) >> 31); |
---|
| 444 | |
---|
| 445 | int bits, val; |
---|
| 446 | do |
---|
| 447 | { |
---|
| 448 | bits = next(31); |
---|
| 449 | val = bits % n; |
---|
| 450 | } |
---|
| 451 | while(bits - val + (n-1) < 0); |
---|
| 452 | return val; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | /** This method is for completness' sake. |
---|
| 456 | Returns a long drawn uniformly from 0 to n-1. Suffice it to say, |
---|
| 457 | n must be > 0, or an IllegalArgumentException is raised. */ |
---|
| 458 | |
---|
| 459 | public long nextLong(final long n) |
---|
| 460 | { |
---|
| 461 | if (n<=0) |
---|
| 462 | throw new IllegalArgumentException("n must be positive, got: " + n); |
---|
| 463 | |
---|
| 464 | long bits, val; |
---|
| 465 | do |
---|
| 466 | { |
---|
| 467 | bits = (nextLong() >>> 1); |
---|
| 468 | val = bits % n; |
---|
| 469 | } |
---|
| 470 | while(bits - val + (n-1) < 0); |
---|
| 471 | return val; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | /** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes |
---|
| 476 | this for us, but what the heck. */ |
---|
| 477 | public double nextDouble() |
---|
| 478 | { |
---|
| 479 | return (((long)next(26) << 27) + next(27)) |
---|
| 480 | / (double)(1L << 53); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | /** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes |
---|
| 484 | this for us, but what the heck. */ |
---|
| 485 | |
---|
| 486 | public float nextFloat() |
---|
| 487 | { |
---|
| 488 | return next(24) / ((float)(1 << 24)); |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | /** A bug fix for all versions of the JDK. The JDK appears to |
---|
| 492 | use all four bytes in an integer as independent byte values! |
---|
| 493 | Totally wrong. I've submitted a bug report. */ |
---|
| 494 | |
---|
| 495 | public void nextBytes(final byte[] bytes) |
---|
| 496 | { |
---|
| 497 | for (int x=0;x<bytes.length;x++) bytes[x] = (byte)next(8); |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | /** For completeness' sake, though it's not in java.util.Random. */ |
---|
| 501 | |
---|
| 502 | public char nextChar() |
---|
| 503 | { |
---|
| 504 | // chars are 16-bit UniCode values |
---|
| 505 | return (char)(next(16)); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | /** For completeness' sake, though it's not in java.util.Random. */ |
---|
| 509 | |
---|
| 510 | public short nextShort() |
---|
| 511 | { |
---|
| 512 | return (short)(next(16)); |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | /** For completeness' sake, though it's not in java.util.Random. */ |
---|
| 516 | |
---|
| 517 | public byte nextByte() |
---|
| 518 | { |
---|
| 519 | return (byte)(next(8)); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | |
---|
| 523 | /** A bug fix for all JDK code including 1.2. nextGaussian can theoretically |
---|
| 524 | ask for the log of 0 and divide it by 0! See Java bug |
---|
| 525 | <a href="http://developer.java.sun.com/developer/bugParade/bugs/4254501.html"> |
---|
| 526 | http://developer.java.sun.com/developer/bugParade/bugs/4254501.html</a> |
---|
| 527 | */ |
---|
| 528 | |
---|
| 529 | synchronized public double nextGaussian() |
---|
| 530 | { |
---|
| 531 | if (__haveNextNextGaussian) |
---|
| 532 | { |
---|
| 533 | __haveNextNextGaussian = false; |
---|
| 534 | return __nextNextGaussian; |
---|
| 535 | } |
---|
| 536 | else |
---|
| 537 | { |
---|
| 538 | double v1, v2, s; |
---|
| 539 | do |
---|
| 540 | { |
---|
| 541 | v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
---|
| 542 | v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
---|
| 543 | s = v1 * v1 + v2 * v2; |
---|
| 544 | } while (s >= 1 || s==0 ); |
---|
| 545 | double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
---|
| 546 | __nextNextGaussian = v2 * multiplier; |
---|
| 547 | __haveNextNextGaussian = true; |
---|
| 548 | return v1 * multiplier; |
---|
| 549 | } |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | /** |
---|
| 553 | * Tests the code. |
---|
| 554 | */ |
---|
| 555 | public static void main(String args[]) |
---|
| 556 | { |
---|
| 557 | int j; |
---|
| 558 | |
---|
| 559 | MersenneTwister r; |
---|
| 560 | |
---|
| 561 | // CORRECTNESS TEST |
---|
| 562 | // COMPARE WITH http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out |
---|
| 563 | |
---|
| 564 | r = new MersenneTwister(new int[]{0x123, 0x234, 0x345, 0x456}); |
---|
| 565 | System.out.println("Output of MersenneTwister with new (2002/1/26) seeding mechanism"); |
---|
| 566 | for (j=0;j<1000;j++) |
---|
| 567 | { |
---|
| 568 | // first, convert the int from signed to "unsigned" |
---|
| 569 | long l = (long)r.nextInt(); |
---|
| 570 | if (l < 0 ) l += 4294967296L; // max int value |
---|
| 571 | String s = String.valueOf(l); |
---|
| 572 | while(s.length() < 10) s = " " + s; // buffer |
---|
| 573 | System.out.print(s + " "); |
---|
| 574 | if (j%5==4) System.out.println(); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | // SPEED TEST |
---|
| 578 | |
---|
| 579 | final long SEED = 4357; |
---|
| 580 | |
---|
| 581 | int xx; long ms; |
---|
| 582 | System.out.println("\nTime to test grabbing 100000000 ints"); |
---|
| 583 | |
---|
| 584 | r = new MersenneTwister(SEED); |
---|
| 585 | ms = System.currentTimeMillis(); |
---|
| 586 | xx=0; |
---|
| 587 | for (j = 0; j < 100000000; j++) |
---|
| 588 | xx += r.nextInt(); |
---|
| 589 | System.out.println("Mersenne Twister: " + (System.currentTimeMillis()-ms) + " Ignore this: " + xx); |
---|
| 590 | |
---|
| 591 | System.out.println("To compare this with java.util.Random, run this same test on MersenneTwisterFast."); |
---|
| 592 | System.out.println("The comparison with Random is removed from MersenneTwister because it is a proper"); |
---|
| 593 | System.out.println("subclass of Random and this unfairly makes some of Random's methods un-inlinable,"); |
---|
| 594 | System.out.println("so it would make Random look worse than it is."); |
---|
| 595 | |
---|
| 596 | // TEST TO COMPARE TYPE CONVERSION BETWEEN |
---|
| 597 | // MersenneTwisterFast.java AND MersenneTwister.java |
---|
| 598 | |
---|
| 599 | |
---|
| 600 | System.out.println("\nGrab the first 1000 booleans"); |
---|
| 601 | r = new MersenneTwister(SEED); |
---|
| 602 | for (j = 0; j < 1000; j++) |
---|
| 603 | { |
---|
| 604 | System.out.print(r.nextBoolean() + " "); |
---|
| 605 | if (j%8==7) System.out.println(); |
---|
| 606 | } |
---|
| 607 | if (!(j%8==7)) System.out.println(); |
---|
| 608 | |
---|
| 609 | System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(double)"); |
---|
| 610 | r = new MersenneTwister(SEED); |
---|
| 611 | for (j = 0; j < 1000; j++) |
---|
| 612 | { |
---|
| 613 | System.out.print(r.nextBoolean((double)(j/999.0)) + " "); |
---|
| 614 | if (j%8==7) System.out.println(); |
---|
| 615 | } |
---|
| 616 | if (!(j%8==7)) System.out.println(); |
---|
| 617 | |
---|
| 618 | System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(float)"); |
---|
| 619 | r = new MersenneTwister(SEED); |
---|
| 620 | for (j = 0; j < 1000; j++) |
---|
| 621 | { |
---|
| 622 | System.out.print(r.nextBoolean((float)(j/999.0f)) + " "); |
---|
| 623 | if (j%8==7) System.out.println(); |
---|
| 624 | } |
---|
| 625 | if (!(j%8==7)) System.out.println(); |
---|
| 626 | |
---|
| 627 | byte[] bytes = new byte[1000]; |
---|
| 628 | System.out.println("\nGrab the first 1000 bytes using nextBytes"); |
---|
| 629 | r = new MersenneTwister(SEED); |
---|
| 630 | r.nextBytes(bytes); |
---|
| 631 | for (j = 0; j < 1000; j++) |
---|
| 632 | { |
---|
| 633 | System.out.print(bytes[j] + " "); |
---|
| 634 | if (j%16==15) System.out.println(); |
---|
| 635 | } |
---|
| 636 | if (!(j%16==15)) System.out.println(); |
---|
| 637 | |
---|
| 638 | byte b; |
---|
| 639 | System.out.println("\nGrab the first 1000 bytes -- must be same as nextBytes"); |
---|
| 640 | r = new MersenneTwister(SEED); |
---|
| 641 | for (j = 0; j < 1000; j++) |
---|
| 642 | { |
---|
| 643 | System.out.print((b = r.nextByte()) + " "); |
---|
| 644 | if (b!=bytes[j]) System.out.print("BAD "); |
---|
| 645 | if (j%16==15) System.out.println(); |
---|
| 646 | } |
---|
| 647 | if (!(j%16==15)) System.out.println(); |
---|
| 648 | |
---|
| 649 | System.out.println("\nGrab the first 1000 shorts"); |
---|
| 650 | r = new MersenneTwister(SEED); |
---|
| 651 | for (j = 0; j < 1000; j++) |
---|
| 652 | { |
---|
| 653 | System.out.print(r.nextShort() + " "); |
---|
| 654 | if (j%8==7) System.out.println(); |
---|
| 655 | } |
---|
| 656 | if (!(j%8==7)) System.out.println(); |
---|
| 657 | |
---|
| 658 | System.out.println("\nGrab the first 1000 ints"); |
---|
| 659 | r = new MersenneTwister(SEED); |
---|
| 660 | for (j = 0; j < 1000; j++) |
---|
| 661 | { |
---|
| 662 | System.out.print(r.nextInt() + " "); |
---|
| 663 | if (j%4==3) System.out.println(); |
---|
| 664 | } |
---|
| 665 | if (!(j%4==3)) System.out.println(); |
---|
| 666 | |
---|
| 667 | System.out.println("\nGrab the first 1000 ints of different sizes"); |
---|
| 668 | r = new MersenneTwister(SEED); |
---|
| 669 | int max = 1; |
---|
| 670 | for (j = 0; j < 1000; j++) |
---|
| 671 | { |
---|
| 672 | System.out.print(r.nextInt(max) + " "); |
---|
| 673 | max *= 2; |
---|
| 674 | if (max <= 0) max = 1; |
---|
| 675 | if (j%4==3) System.out.println(); |
---|
| 676 | } |
---|
| 677 | if (!(j%4==3)) System.out.println(); |
---|
| 678 | |
---|
| 679 | System.out.println("\nGrab the first 1000 longs"); |
---|
| 680 | r = new MersenneTwister(SEED); |
---|
| 681 | for (j = 0; j < 1000; j++) |
---|
| 682 | { |
---|
| 683 | System.out.print(r.nextLong() + " "); |
---|
| 684 | if (j%3==2) System.out.println(); |
---|
| 685 | } |
---|
| 686 | if (!(j%3==2)) System.out.println(); |
---|
| 687 | |
---|
| 688 | System.out.println("\nGrab the first 1000 longs of different sizes"); |
---|
| 689 | r = new MersenneTwister(SEED); |
---|
| 690 | long max2 = 1; |
---|
| 691 | for (j = 0; j < 1000; j++) |
---|
| 692 | { |
---|
| 693 | System.out.print(r.nextLong(max2) + " "); |
---|
| 694 | max2 *= 2; |
---|
| 695 | if (max2 <= 0) max2 = 1; |
---|
| 696 | if (j%4==3) System.out.println(); |
---|
| 697 | } |
---|
| 698 | if (!(j%4==3)) System.out.println(); |
---|
| 699 | |
---|
| 700 | System.out.println("\nGrab the first 1000 floats"); |
---|
| 701 | r = new MersenneTwister(SEED); |
---|
| 702 | for (j = 0; j < 1000; j++) |
---|
| 703 | { |
---|
| 704 | System.out.print(r.nextFloat() + " "); |
---|
| 705 | if (j%4==3) System.out.println(); |
---|
| 706 | } |
---|
| 707 | if (!(j%4==3)) System.out.println(); |
---|
| 708 | |
---|
| 709 | System.out.println("\nGrab the first 1000 doubles"); |
---|
| 710 | r = new MersenneTwister(SEED); |
---|
| 711 | for (j = 0; j < 1000; j++) |
---|
| 712 | { |
---|
| 713 | System.out.print(r.nextDouble() + " "); |
---|
| 714 | if (j%3==2) System.out.println(); |
---|
| 715 | } |
---|
| 716 | if (!(j%3==2)) System.out.println(); |
---|
| 717 | |
---|
| 718 | System.out.println("\nGrab the first 1000 gaussian doubles"); |
---|
| 719 | r = new MersenneTwister(SEED); |
---|
| 720 | for (j = 0; j < 1000; j++) |
---|
| 721 | { |
---|
| 722 | System.out.print(r.nextGaussian() + " "); |
---|
| 723 | if (j%3==2) System.out.println(); |
---|
| 724 | } |
---|
| 725 | if (!(j%3==2)) System.out.println(); |
---|
| 726 | |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | } |
---|