src/java.base/share/classes/java/util/SplittableRandom.java
branchJDK-8193209-branch
changeset 57437 f02ffcb61dce
parent 57388 b1e6bc96af3d
child 57442 4b19f34bbe1b
equal deleted inserted replaced
57436:b0c958c0e6c6 57437:f02ffcb61dce
    24  */
    24  */
    25 package java.util;
    25 package java.util;
    26 
    26 
    27 import java.math.BigInteger;
    27 import java.math.BigInteger;
    28 import java.util.concurrent.atomic.AtomicLong;
    28 import java.util.concurrent.atomic.AtomicLong;
       
    29 import java.util.random.AbstractSplittableRNG;
       
    30 import java.util.random.RNGSupport;
       
    31 import java.util.random.SplittableRNG;
    29 
    32 
    30 /**
    33 /**
    31  * A generator of uniform pseudorandom values applicable for use in
    34  * A generator of uniform pseudorandom values applicable for use in
    32  * (among other contexts) isolated parallel computations that may
    35  * (among other contexts) isolated parallel computations that may
    33  * generate subtasks. Class {@code SplittableRandom} supports methods for
    36  * generate subtasks. Class {@code SplittableRandom} supports methods for
    77  *
    80  *
    78  * @author  Guy Steele
    81  * @author  Guy Steele
    79  * @author  Doug Lea
    82  * @author  Doug Lea
    80  * @since   1.8
    83  * @since   1.8
    81  */
    84  */
    82 public final class SplittableRandom extends AbstractSplittableRng {
    85 public final class SplittableRandom extends AbstractSplittableRNG {
    83 
    86 
    84     /*
    87     /*
    85      * Implementation Overview.
    88      * Implementation Overview.
    86      *
    89      *
    87      * This algorithm was inspired by the "DotMix" algorithm by
    90      * This algorithm was inspired by the "DotMix" algorithm by
   191     }
   194     }
   192 
   195 
   193     /**
   196     /**
   194      * Returns the gamma value to use for a new split instance.
   197      * Returns the gamma value to use for a new split instance.
   195      * Uses the 64bit mix function from MurmurHash3.
   198      * Uses the 64bit mix function from MurmurHash3.
   196      * https://github.com/aappleby/smhasher/wiki/MurmurHash3     
   199      * https://github.com/aappleby/smhasher/wiki/MurmurHash3
   197      */
   200      */
   198     private static long mixGamma(long z) {
   201     private static long mixGamma(long z) {
   199         z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
   202         z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
   200         z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
   203         z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
   201         z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
   204         z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
   211     }
   214     }
   212 
   215 
   213     /**
   216     /**
   214      * The seed generator for default constructors.
   217      * The seed generator for default constructors.
   215      */
   218      */
   216     private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
   219     private static final AtomicLong defaultGen = new AtomicLong(RNGSupport.initialSeed());
   217     
   220 
   218     /* ---------------- public methods ---------------- */
   221     /* ---------------- public methods ---------------- */
   219 
   222 
   220     /**
   223     /**
   221      * Creates a new SplittableRandom instance using the specified
   224      * Creates a new SplittableRandom instance using the specified
   222      * initial seed. SplittableRandom instances created with the same
   225      * initial seed. SplittableRandom instances created with the same
   239         this.seed = mix64(s);
   242         this.seed = mix64(s);
   240         this.gamma = mixGamma(s + GOLDEN_GAMMA);
   243         this.gamma = mixGamma(s + GOLDEN_GAMMA);
   241     }
   244     }
   242 
   245 
   243     //    public SplittableRandom copy() { return new SplittableRandom(seed, gamma); }
   246     //    public SplittableRandom copy() { return new SplittableRandom(seed, gamma); }
   244     
   247 
   245     /**
   248     /**
   246      * Constructs and returns a new SplittableRandom instance that
   249      * Constructs and returns a new SplittableRandom instance that
   247      * shares no mutable state with this instance. However, with very
   250      * shares no mutable state with this instance. However, with very
   248      * high probability, the set of values collectively generated by
   251      * high probability, the set of values collectively generated by
   249      * the two objects has the same statistical properties as if the
   252      * the two objects has the same statistical properties as if the
   258      */
   261      */
   259     public SplittableRandom split() {
   262     public SplittableRandom split() {
   260         return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
   263         return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
   261     }
   264     }
   262 
   265 
   263     public SplittableRandom split(SplittableRng source) {
   266     public SplittableRandom split(SplittableRNG source) {
   264         return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
   267         return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
   265     }
   268     }
   266 
   269 
   267     /**
   270     /**
   268      * Returns a pseudorandom {@code int} value.
   271      * Returns a pseudorandom {@code int} value.