?? mersennetwister.java
字號:
/*** just so we're synchronized.*/ out.defaultWriteObject(); } private synchronized void readObject (final ObjectInputStream in) throws IOException, ClassNotFoundException {/*** just so we're synchronized.*/ in.defaultReadObject(); }/*** This method is missing from jdk 1.0.x and below. JDK 1.1 includes** this for us, but what the heck.*/ public boolean nextBoolean() {return next(1) != 0;}/*** This generates a coin flip with a probability <tt>probability</tt>** of returning true, else returning false. <tt>probability</tt>** must be between 0.0 and 1.0, inclusive. Not as precise a random** real event as nextBoolean(double), but twice as fast. To explicitly** use this, remember you may need to cast to float first.*/ public boolean nextBoolean (final float probability) { if (probability < 0.0f || probability > 1.0f) { throw new IllegalArgumentException ( "probability must be between 0.0 and 1.0 inclusive." ); } return nextFloat() < probability; }/*** This generates a coin flip with a probability <tt>probability</tt>** of returning true, else returning false. <tt>probability</tt>** must be between 0.0 and 1.0, inclusive.*/ public boolean nextBoolean (final double probability) { if (probability < 0.0 || probability > 1.0) { throw new IllegalArgumentException ( "probability must be between 0.0 and 1.0 inclusive." ); } return nextDouble() < probability; }/*** This method is missing from JDK 1.1 and below. JDK 1.2 includes** this for us, but what the heck.*/ public int nextInt(final int n) { if (n<=0) {/*** Fixed error message, was "must be >= 0". KPD.*/ throw new IllegalArgumentException("n must be > 0"); } if ((n & -n) == n) { return (int)((n * (long)next(31)) >> 31); } int bits, val; do { bits = next(31); val = bits % n; } while(bits - val + (n-1) < 0); return val; }/*** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes this** for us, but what the heck.*/ public double nextDouble() { return (((long)next(26) << 27) + next(27)) / (double)(1L << 53); }/*** Added by KPD.*/ public double nextDouble(double lo, double hi) { return lo + (hi - lo) * this.nextDouble(); }/*** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes this** for us, but what the heck.*/ public float nextFloat() { return next(24) / ((float)(1 << 24)); }/*** A bug fix for all versions of the JDK. The JDK appears to** use all four bytes in an integer as independent byte values!** Totally wrong. I've submitted a bug report.*/ public void nextBytes(final byte[] bytes) { for ( int x=0; x<bytes.length; x++ ) { bytes[x] = (byte)next(8); } }/*** For completeness' sake, though it's not in java.util.Random.*/ public char nextChar() {/*** chars are 16-bit UniCode values*/ return (char)(next(16)); }/*** For completeness' sake, though it's not in java.util.Random.*/ public short nextShort() { return (short)(next(16)); }/*** For completeness' sake, though it's not in java.util.Random.*/ public byte nextByte() { return (byte)(next(8)); }/*** A bug fix for all JDK code including 1.2.** nextGaussian can theoretically ask for** the log of 0 and divide it by 0! See Java bug** <a href="http://developer.java.sun.com/developer/bugParade/bugs/4254501.html">** http://developer.java.sun.com/developer/bugParade/bugs/4254501.html</a>*/ synchronized public double nextGaussian() { if (__haveNextNextGaussian) { __haveNextNextGaussian = false; return __nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 s = v1 * v1 + v2 * v2; } while (s >= 1 || s==0 ); double multiplier = Math.sqrt(-2 * Math.log(s)/s); __nextNextGaussian = v2 * multiplier; __haveNextNextGaussian = true; return v1 * multiplier; } }/*** Tests the code.*//*** Not any more it doesn't! KPD.*/// public static void main(String args[])// {// int j;//// MersenneTwister r;//// // UNCOMMENT THIS TO TEST FOR CORRECTNESS// // WITH ORIGINAL ALGORITHM// /*// r = new MersenneTwister(4357);// r.setSeedOld(4357)// System.out.println("Output of MersenneTwister, old style");// for (j=0;j<1000;j++)// {// // first, convert the int from signed to "unsigned"// long l = (long)r.nextInt();// if (l < 0 ) l += 4294967296L; // max int value// String s = String.valueOf(l);// while(s.length() < 10) s = " " + s; // buffer// System.out.print(s + " ");// if (j%8==7) System.out.println();// }// *///// // UNCOMMENT THIS TO TEST FOR CORRECTNESS WITH// // NEW VERSION MT19937 1999/10/28// // COMPARE WITH http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.out//// /*// r = new MersenneTwister(4357);// System.out.println// (// "Output of MersenneTwister with new (1999/10/28) seeding mechanism"// );// for (j=0;j<1000;j++)// {// // first, convert the int from signed to "unsigned"// long l = (long)r.nextInt();// if (l < 0 ) l += 4294967296L; // max int value// String s = String.valueOf(l);// while(s.length() < 10) s = " " + s; // buffer// System.out.print(s + " ");// if (j%5==4) System.out.println();// }// *///// // UNCOMMENT THIS TO TEST FOR SPEED// /*// r = new MersenneTwister();// System.out.println("\nTime to test grabbing 10000000 ints");// long ms = System.currentTimeMillis();// int xx=0;// for (j = 0; j < 10000000; j++)// xx += r.nextInt();// System.out.println// (// "Mersenne Twister: "// +// (// System.currentTimeMillis() - ms// + " Ignore this: "// + xx// )// );//// Random rr = new Random(1);// xx = 0;// ms = System.currentTimeMillis();// for (j = 0; j < 10000000; j++)// xx += rr.nextInt();// System.out.println// (// "java.util.Random: "// +// (// System.currentTimeMillis()-ms// + " Ignore this: "// + xx// )// );// *///// // UNCOMMENT THIS TO DO TEST DIFFERENT TYPE OUTPUTS// // THIS CAN BE USED TO COMPARE THE DIFFERENCE BETWEEN// // MersenneTwisterFast.java AND MersenneTwister.java////// System.out.println("\nGrab the first 1000 booleans");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextBoolean() + " ");// if (j%8==7) System.out.println();// }// if (!(j%8==7)) System.out.println();//// System.out.println// (// "\nGrab 1000 booleans of increasing probability using nextBoolean(double)"// );// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextBoolean((double)(j/999.0)) + " ");// if (j%8==7) { System.out.println(); }// }// if (!(j%8==7)) { System.out.println(); }//// System.out.println// (// "\nGrab 1000 booleans of increasing probability using nextBoolean(float)"// );// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextBoolean((float)(j/999.0f)) + " ");// if (j%8==7) { System.out.println(); }// }// if (!(j%8==7)) { System.out.println(); }//// byte[] bytes = new byte[1000];// System.out.println("\nGrab the first 1000 bytes using nextBytes");// r = new MersenneTwister();// r.nextBytes(bytes);// for (j = 0; j < 1000; j++)// {// System.out.print(bytes[j] + " ");// if (j%16==15) { System.out.println(); }// }// if (!(j%16==15)) { System.out.println(); }//// byte b;// System.out.println// (// "\nGrab the first 1000 bytes -- must be same as nextBytes"// );// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print((b = r.nextByte()) + " ");// if (b!=bytes[j]) { System.out.print("BAD "); }// if (j%16==15) { System.out.println(); }// }// if (!(j%16==15)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 shorts");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextShort() + " ");// if (j%8==7) { System.out.println(); }// }// if (!(j%8==7)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 ints");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextInt() + " ");// if (j%4==3) { System.out.println(); }// }// if (!(j%4==3)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 ints of different sizes");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextInt(j+1) + " ");// if (j%4==3) { System.out.println(); }// }// if (!(j%4==3)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 longs");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextLong() + " ");// if (j%3==2) { System.out.println(); }// }// if (!(j%3==2)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 floats");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextFloat() + " ");// if (j%4==3) { System.out.println(); }// }// if (!(j%4==3)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 doubles");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextDouble() + " ");// if (j%3==2) { System.out.println(); }// }// if (!(j%3==2)) { System.out.println(); }//// System.out.println("\nGrab the first 1000 gaussian doubles");// r = new MersenneTwister();// for (j = 0; j < 1000; j++)// {// System.out.print(r.nextGaussian() + " ");// if (j%3==2) { System.out.println(); }// }// if (!(j%3==2)) { System.out.println(); }//// }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -