ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/SplittableRandom.java
(Generate patch)

Comparing jsr166/src/main/java/util/SplittableRandom.java (file contents):
Revision 1.21 by dl, Thu Sep 19 23:19:43 2013 UTC vs.
Revision 1.24 by dl, Mon Oct 7 10:54:27 2013 UTC

# Line 25 | Line 25
25  
26   package java.util;
27  
28 import java.security.SecureRandom;
28   import java.net.NetworkInterface;
30 import java.util.Enumeration;
29   import java.util.concurrent.atomic.AtomicLong;
30   import java.util.function.IntConsumer;
31   import java.util.function.LongConsumer;
# Line 89 | Line 87 | import java.util.stream.DoubleStream;
87   * @author  Doug Lea
88   * @since   1.8
89   */
90 < public class SplittableRandom {
90 > public final class SplittableRandom {
91  
92      /*
93       * Implementation Overview.
# Line 112 | Line 110 | public class SplittableRandom {
110       * For nextLong, the mix64 function is based on David Stafford's
111       * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
112       * "Mix13" variant of the "64-bit finalizer" function in Austin
113 <     * Appleby's MurmurHash3 algorithm See
114 <     * http://code.google.com/p/smhasher/wiki/MurmurHash3 . The mix32
113 >     * Appleby's MurmurHash3 algorithm (see
114 >     * http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32
115       * function is based on Stafford's Mix04 mix function, but returns
116       * the upper 32 bits cast as int.
117       *
# Line 168 | Line 166 | public class SplittableRandom {
166       * The least non-zero value returned by nextDouble(). This value
167       * 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);
169 >    private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53);
170  
171      /**
172       * The seed. Updated only via method nextSeed.
# Line 192 | Line 190 | public class SplittableRandom {
190       * Computes Stafford variant 13 of 64bit mix function.
191       */
192      private static long mix64(long z) {
193 <        z *= 0xbf58476d1ce4e5b9L;
196 <        z = (z ^ (z >>> 32)) * 0x94d049bb133111ebL;
197 <        return z ^ (z >>> 32);
198 <    }
199 <
200 <    private static long xmix64(long z) {
201 <        z *= 0xbf58476d1ce4e5b9L;
193 >        z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
194          z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
195          return z ^ (z >>> 31);
196      }
# Line 207 | Line 199 | public class SplittableRandom {
199       * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
200       */
201      private static int mix32(long z) {
202 <        z *= 0x62a9d9ed799705f5L;
202 >        z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
203          return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
204      }
205  
# Line 215 | Line 207 | public class SplittableRandom {
207       * Returns the gamma value to use for a new split instance.
208       */
209      private static long mixGamma(long z) {
210 <        z *= 0xff51afd7ed558ccdL;                   // MurmurHash3 mix constants
210 >        z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
211          z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
212          z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
213          int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
# Line 248 | Line 240 | public class SplittableRandom {
240          long h = 0L;
241          try {
242              Enumeration<NetworkInterface> ifcs =
243 <                NetworkInterface.getNetworkInterfaces();
243 >                    NetworkInterface.getNetworkInterfaces();
244              boolean retry = false; // retry once if getHardwareAddress is null
245              while (ifcs.hasMoreElements()) {
246                  NetworkInterface ifc = ifcs.nextElement();
# Line 381 | Line 373 | public class SplittableRandom {
373       * @return a pseudorandom value
374       */
375      final double internalNextDouble(double origin, double bound) {
376 <        double r = (nextLong() >>> 11) * DOUBLE_ULP;
376 >        double r = (nextLong() >>> 11) * DOUBLE_UNIT;
377          if (origin < bound) {
378              r = r * (bound - origin) + origin;
379              if (r >= bound) // correct for rounding
# Line 410 | Line 402 | public class SplittableRandom {
402       * may, and typically does, vary across program invocations.
403       */
404      public SplittableRandom() { // emulate defaultGen.split()
405 <        long s = defaultGen.getAndAdd(2*GOLDEN_GAMMA);
405 >        long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
406          this.seed = mix64(s);
407          this.gamma = mixGamma(s + GOLDEN_GAMMA);
408      }
# Line 545 | Line 537 | public class SplittableRandom {
537       *         (inclusive) and one (exclusive)
538       */
539      public double nextDouble() {
540 <        return (mix64(nextSeed()) >>> 11) * DOUBLE_ULP;
540 >        return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
541      }
542  
543      /**
# Line 560 | Line 552 | public class SplittableRandom {
552      public double nextDouble(double bound) {
553          if (!(bound > 0.0))
554              throw new IllegalArgumentException(BadBound);
555 <        double result = (mix64(nextSeed()) >>> 11) * DOUBLE_ULP * bound;
555 >        double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
556          return (result < bound) ?  result : // correct for rounding
557              Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
558      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines