[cvs] / jsr166 / src / main / java / util / SplittableRandom.java Repository: Repository Listing cvs

# Diff of /jsr166/src/main/java/util/SplittableRandom.java

revision 1.23, Fri Sep 20 14:06:46 2013 UTC revision 1.24, Mon Oct 7 10:54:27 2013 UTC
# Line 110  Line 110
110       * For nextLong, the mix64 function is based on David Stafford's       * For nextLong, the mix64 function is based on David Stafford's
111       * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)       * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
112       * "Mix13" variant of the "64-bit finalizer" function in Austin       * "Mix13" variant of the "64-bit finalizer" function in Austin
113       * Appleby's MurmurHash3 algorithm See       * Appleby's MurmurHash3 algorithm (see
114       * http://code.google.com/p/smhasher/wiki/MurmurHash3 . The mix32       * http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32
115       * function is based on Stafford's Mix04 mix function, but returns       * function is based on Stafford's Mix04 mix function, but returns
116       * the upper 32 bits cast as int.       * the upper 32 bits cast as int.
117       *       *
# Line 166  Line 166
166       * The least non-zero value returned by nextDouble(). This value       * The least non-zero value returned by nextDouble(). This value
167       * is scaled by a random value of 53 bits to produce a result.       * is scaled by a random value of 53 bits to produce a result.
168       */       */
169      private static final double DOUBLE_ULP = 1.0 / (1L << 53);      private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53);
170
171      /**      /**
172       * The seed. Updated only via method nextSeed.       * The seed. Updated only via method nextSeed.
# Line 190  Line 190
190       * Computes Stafford variant 13 of 64bit mix function.       * Computes Stafford variant 13 of 64bit mix function.
191       */       */
192      private static long mix64(long z) {      private static long mix64(long z) {
193          z *= 0xbf58476d1ce4e5b9L;          z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
194          z = (z ^ (z >>> 32)) * 0x94d049bb133111ebL;          z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
195          return z ^ (z >>> 32);          return z ^ (z >>> 31);
196      }      }
197
198      /**      /**
199       * Returns the 32 high bits of Stafford variant 4 mix64 function as int.       * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
200       */       */
201      private static int mix32(long z) {      private static int mix32(long z) {
202          z *= 0x62a9d9ed799705f5L;          z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
203          return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);          return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
204      }      }
205
# Line 207  Line 207
207       * Returns the gamma value to use for a new split instance.       * Returns the gamma value to use for a new split instance.
208       */       */
209      private static long mixGamma(long z) {      private static long mixGamma(long z) {
210          z *= 0xff51afd7ed558ccdL;                   // MurmurHash3 mix constants          z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
211          z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;          z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
212          z = (z ^ (z >>> 33)) | 1L;                  // force to be odd          z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
213          int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions          int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
# Line 373  Line 373
373       * @return a pseudorandom value       * @return a pseudorandom value
374       */       */
375      final double internalNextDouble(double origin, double bound) {      final double internalNextDouble(double origin, double bound) {
376          double r = (nextLong() >>> 11) * DOUBLE_ULP;          double r = (nextLong() >>> 11) * DOUBLE_UNIT;
377          if (origin < bound) {          if (origin < bound) {
378              r = r * (bound - origin) + origin;              r = r * (bound - origin) + origin;
379              if (r >= bound) // correct for rounding              if (r >= bound) // correct for rounding
# Line 537  Line 537
537       *         (inclusive) and one (exclusive)       *         (inclusive) and one (exclusive)
538       */       */
539      public double nextDouble() {      public double nextDouble() {
540          return (mix64(nextSeed()) >>> 11) * DOUBLE_ULP;          return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
541      }      }
542
543      /**      /**
# Line 552  Line 552
552      public double nextDouble(double bound) {      public double nextDouble(double bound) {
553          if (!(bound > 0.0))          if (!(bound > 0.0))
554              throw new IllegalArgumentException(BadBound);              throw new IllegalArgumentException(BadBound);
555          double result = (mix64(nextSeed()) >>> 11) * DOUBLE_ULP * bound;          double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
556          return (result < bound) ?  result : // correct for rounding          return (result < bound) ?  result : // correct for rounding
557              Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);              Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
558      }      }

Legend:
 Removed from v.1.23 changed lines Added in v.1.24

 Doug Lea ViewVC Help Powered by ViewVC 1.0.8