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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.14, Mon Aug 5 13:58:02 2013 UTC revision 1.15, Fri Aug 9 12:12:10 2013 UTC
# Line 25  Line 25 
25    
26  package java.util;  package java.util;
27    
28  import java.security.SecureRandom;  import java.net.InetAddress;
29  import java.util.concurrent.atomic.AtomicLong;  import java.util.concurrent.atomic.AtomicLong;
30  import java.util.Spliterator;  import java.util.Spliterator;
31  import java.util.function.IntConsumer;  import java.util.function.IntConsumer;
# Line 84  Line 84 
84  public class SplittableRandom {  public class SplittableRandom {
85    
86      /*      /*
      * File organization: First the non-public methods that constitute  
      * the main algorithm, then the main public methods, followed by  
      * some custom spliterator classes needed for stream methods.  
      *  
      * Credits: Primary algorithm and code by Guy Steele.  Stream  
      * support methods by Doug Lea.  Documentation jointly produced  
      * with additional help from Brian Goetz.  
      */  
   
     /*  
87       * Implementation Overview.       * Implementation Overview.
88       *       *
89       * This algorithm was inspired by the "DotMix" algorithm by       * This algorithm was inspired by the "DotMix" algorithm by
90       * Leiserson, Schardl, and Sukha "Deterministic Parallel       * Leiserson, Schardl, and Sukha "Deterministic Parallel
91       * Random-Number Generation for Dynamic-Multithreading Platforms",       * Random-Number Generation for Dynamic-Multithreading Platforms",
92       * PPoPP 2012, but improves and extends it in several ways.       * PPoPP 2012, as well as those in "Parallel random numbers: as
93       *       * easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011.  It
94       * The primary update step (see method nextSeed()) is simply to       * differs mainly in simplifying and cheapening operations.
95       * add a constant ("gamma") to the current seed, modulo a prime       *
96       * ("George"). However, the nextLong and nextInt methods do not       * The primary update step (method nextSeed()) is to add a
97       * return this value, but instead the results of bit-mixing       * constant ("gamma") to the current (64 bit) seed, forming a
98       * transformations that produce more uniformly distributed       * simple sequence.  The seed and the gamma values for any two
99       * sequences.       * SplittableRandom instances are highly likely to be different.
100       *       *
101       * "George" is the otherwise nameless (because it cannot be       * Methods nextLong, nextInt, and derivatives do not return the
102       * represented) prime number 2^64+13. Using a prime number larger       * sequence (seed) values, but instead a hash-like bit-mix of
103       * than can fit in a long ensures that all possible long values       * their bits, producing more independently distributed sequences.
104       * can occur, plus 13 others that just get skipped over when they       * For nextLong, the mix64 bit-mixing function computes the same
105       * are encountered; see method addGammaModGeorge. For this to       * value as the "64-bit finalizer" function in Austin Appleby's
106       * work, initial gamma values must be at least 13.       * MurmurHash3 algorithm.  See
      *  
      * The mix64 bit-mixing function called by nextLong and other  
      * methods computes the same value as the "64-bit finalizer"  
      * function in Austin Appleby's MurmurHash3 algorithm.  See  
107       * http://code.google.com/p/smhasher/wiki/MurmurHash3 , which       * http://code.google.com/p/smhasher/wiki/MurmurHash3 , which
108       * comments: "The constants for the finalizers were generated by a       * comments: "The constants for the finalizers were generated by a
109       * simple simulated-annealing algorithm, and both avalanche all       * simple simulated-annealing algorithm, and both avalanche all
110       * bits of 'h' to within 0.25% bias."       * bits of 'h' to within 0.25% bias." The mix32 function is
111       *       * equivalent to (int)(mix64(seed) >>> 32), but faster because it
112       * The value of gamma differs for each instance across a series of       * omits a step that doesn't contribute to result.
113       * splits, and is generated using an independent variant of the       *
114       * same algorithm, but operating across calls to split(), not       * The split operation uses the current generator to form the seed
115       * calls to nextSeed(): Each instance carries the state of this       * and gamma for another SplittableRandom.  To conservatively
116       * generator as nextSplit. Gammas are treated as 57bit values,       * avoid potential correlations between seed and value generation,
117       * advancing by adding GAMMA_GAMMA mod GAMMA_PRIME, and bit-mixed       * gamma selection (method nextGamma) uses the "Mix13" constants
118       * with a 57-bit version of mix, using the "Mix13" multiplicative       * for MurmurHash3 described by David Stafford
119       * constants for MurmurHash3 described by David Stafford       * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
120       * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html).       * To avoid potential weaknesses in bit-mixing transformations, we
121       * The value of GAMMA_GAMMA is arbitrary (except must be at least       * restrict gammas to odd values with at least 12 and no more than
122       * 13 and less than GAMMA_PRIME), but because it serves as the       * 52 bits set.  Rather than rejecting candidates with too few or
123       * base of split sequences, should be subject to validation of       * too many bits set, method nextGamma flips some bits (which has
124       * consequent random number quality metrics.       * the effect of mapping at most 4 to any given gamma value).
125       *       * This reduces the effective set of 64bit odd gamma values by
126       * The mix32 function used for nextInt just consists of two of the       * about 2<sup>14</sup>, a very tiny percentage, and serves as an
127       * five lines of mix64; avalanche testing shows that the 64-bit       * automated screening for sequence constant selection that is
128       * result has its top 32 bits avalanched well, though not the       * left as an empirical decision in some other hashing and crypto
129       * bottom 32 bits.  DieHarder tests show that it is adequate for       * algorithms.
130       * generating one random int from the 64-bit result of nextSeed.       *
131         * The resulting generator thus transforms a sequence in which
132         * (typically) many bits change on each step, with an inexpensive
133         * mixer with good (but less than cryptographically secure)
134         * avalanching.
135         *
136         * The default (no-argument) constructor, in essence, invokes
137         * split() for a common "seeder" SplittableRandom.  Unlike other
138         * cases, this split must be performed in a thread-safe manner, so
139         * we use an AtomicLong to represent the seed rather than use an
140         * explicit SplittableRandom. To bootstrap the seeder, we start
141         * off using a seed based on current time and host. This serves as
142         * a slimmed-down (and insecure) variant of SecureRandom that also
143         * avoids stalls that may occur when using /dev/random.
144         *
145         * It is a relatively simple matter to apply the basic design here
146         * to use 128 bit seeds. However, emulating 128bit arithmetic and
147         * carrying around twice the state add more overhead than appears
148         * warranted for current usages.
149       *       *
150       * Support for the default (no-argument) constructor relies on an       * File organization: First the non-public methods that constitute
151       * AtomicLong (defaultSeedGenerator) to help perform the       * the main algorithm, then the main public methods, followed by
152       * equivalent of a split of a statically constructed       * some custom spliterator classes needed for stream methods.
      * SplittableRandom. Unlike other cases, this split must be  
      * performed in a thread-safe manner. We use  
      * AtomicLong.compareAndSet as the (typically) most efficient  
      * mechanism. To bootstrap, we start off using a SecureRandom  
      * initial default seed, and update using a fixed  
      * DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA,  
      * not 0, for its splitSeed argument (addGammaModGeorge(0,  
      * GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from  
      * this root generator, even though the root is not explicitly  
      * represented as a SplittableRandom.  
      */  
   
     /**  
      * The prime modulus for gamma values.  
      */  
     private static final long GAMMA_PRIME = (1L << 57) - 13L;  
   
     /**  
      * The value for producing new gamma values. Must be greater or  
      * equal to 13 and less than GAMMA_PRIME. Otherwise, the value is  
      * arbitrary subject to validation of the resulting statistical  
      * quality of splits.  
      */  
     private static final long GAMMA_GAMMA = 0x00aae38294f712aabL;  
   
     /**  
      * The seed update value for default constructors.  Must be  
      * greater or equal to 13. Otherwise, the value is arbitrary  
      * subject to quality checks.  
153       */       */
     private static final long DEFAULT_SEED_GAMMA = 0x9e3779b97f4a7c15L;  
154    
155      /**      /**
156       * The value 13 with 64bit sign bit set. Used in the signed       * The initial gamma value for (unsplit) SplittableRandoms. Must
157       * comparison in addGammaModGeorge.       * be odd with at least 12 and no more than 52 bits set. Currently
158         * set to the golden ratio scaled to 64bits.
159       */       */
160      private static final long BOTTOM13 = 0x800000000000000DL;      private static final long INITIAL_GAMMA = 0x9e3779b97f4a7c15L;
161    
162      /**      /**
163       * The least non-zero value returned by nextDouble(). This value       * The least non-zero value returned by nextDouble(). This value
# Line 191  Line 166 
166      private static final double DOUBLE_UNIT = 1.0 / (1L << 53);      private static final double DOUBLE_UNIT = 1.0 / (1L << 53);
167    
168      /**      /**
169       * The next seed for default constructors.       * The seed. Updated only via method nextSeed.
      */  
     private static final AtomicLong defaultSeedGenerator =  
         new AtomicLong(getInitialDefaultSeed());  
   
     /**  
      * The seed, updated only via method nextSeed.  
170       */       */
171      private long seed;      private long seed;
172    
173      /**      /**
174       * The constant value added to seed (mod George) on each update.       * The step value.
175       */       */
176      private final long gamma;      private final long gamma;
177    
178      /**      /**
179       * The next seed to use for splits. Propagated using       * Internal constructor used by all others except default constructor.
      * addGammaModGeorge across instances.  
      */  
     private final long nextSplit;  
   
     /**  
      * Adds the given gamma value, g, to the given seed value s, mod  
      * George (2^64+13). We regard s and g as unsigned values  
      * (ranging from 0 to 2^64-1). We add g to s either once or twice  
      * (mod George) as necessary to produce an (unsigned) result less  
      * than 2^64.  We require that g must be at least 13. This  
      * guarantees that if (s+g) mod George >= 2^64 then (s+g+g) mod  
      * George < 2^64; thus we need only a conditional, not a loop,  
      * to be sure of getting a representable value.  
      *  
      * Because Java comparison operators are signed, we implement this  
      * by conceptually offsetting seed values downwards by 2^63, so  
      * 0..13 is represented as Long.MIN_VALUE..BOTTOM13.  
      *  
      * @param s a seed value, viewed as a signed long  
      * @param g a gamma value, 13 <= g (as unsigned)  
180       */       */
181      private static long addGammaModGeorge(long s, long g) {      private SplittableRandom(long seed, long gamma) {
182          long p = s + g;          this.seed = seed;
183          return (p >= s) ? p : ((p >= BOTTOM13) ? p  : p + g) - 13L;          this.gamma = gamma;
184      }      }
185    
186      /**      /**
187       * Returns a bit-mixed transformation of its argument.       * Computes MurmurHash3 64bit mix function.
      * See above for explanation.  
188       */       */
189      private static long mix64(long z) {      private static long mix64(long z) {
190          z ^= (z >>> 33);          z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
191          z *= 0xff51afd7ed558ccdL;          z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
192          z ^= (z >>> 33);          return z ^ (z >>> 33);
         z *= 0xc4ceb9fe1a85ec53L;  
         z ^= (z >>> 33);  
         return z;  
193      }      }
194    
195      /**      /**
196       * Returns a bit-mixed int transformation of its argument.       * Returns the 32 high bits of mix64(z) as int.
      * See above for explanation.  
197       */       */
198      private static int mix32(long z) {      private static int mix32(long z) {
199          z ^= (z >>> 33);          z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
200          z *= 0xc4ceb9fe1a85ec53L;          return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32);
         return (int)(z >>> 32);  
201      }      }
202    
203      /**      /**
204       * Returns a 57-bit mixed transformation of its argument.  See       * Returns the gamma value to use for a new split instance.
      * above for explanation.  
205       */       */
206      private static long mix57(long z) {      private static long nextGamma(long z) {
207          z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;          z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; // Stafford "Mix13"
         z &= 0x01FFFFFFFFFFFFFFL;  
208          z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;          z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
209          z &= 0x01FFFFFFFFFFFFFFL;          z = (z ^ (z >>> 31)) | 1L; // force to be odd
210          z ^= (z >>> 31);          int n = Long.bitCount(z);  // ensure enough 0 and 1 bits
211          return z;          return (n < 12 || n > 52) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
     }  
   
     /**  
      * Internal constructor used by all other constructors and by  
      * method split. Establishes the initial seed for this instance,  
      * and uses the given splitSeed to establish gamma, as well as the  
      * nextSplit to use by this instance. The loop to skip ineligible  
      * gammas very rarely iterates, and does so at most 13 times.  
      */  
     private SplittableRandom(long seed, long splitSeed) {  
         this.seed = seed;  
         long s = splitSeed, g;  
         do { // ensure gamma >= 13, considered as an unsigned integer  
             s += GAMMA_GAMMA;  
             if (s >= GAMMA_PRIME)  
                 s -= GAMMA_PRIME;  
             g = mix57(s);  
         } while (g < 13L);  
         this.gamma = g;  
         this.nextSplit = s;  
212      }      }
213    
214      /**      /**
215       * Updates in-place and returns seed.       * Adds gamma to seed.
      * See above for explanation.  
216       */       */
217      private long nextSeed() {      private long nextSeed() {
218          return seed = addGammaModGeorge(seed, gamma);          return seed += gamma;
219      }      }
220    
221      /**      /**
222       * Atomically updates and returns next seed for default constructor.       * The seed generator for default constructors.
223       */       */
224      private static long nextDefaultSeed() {      private static final AtomicLong seeder =
225          long oldSeed, newSeed;          new AtomicLong(mix64((((long)hashedHostAddress()) << 32) ^
226          do {                               System.currentTimeMillis()) ^
227              oldSeed = defaultSeedGenerator.get();                         mix64(System.nanoTime()));
             newSeed = addGammaModGeorge(oldSeed, DEFAULT_SEED_GAMMA);  
         } while (!defaultSeedGenerator.compareAndSet(oldSeed, newSeed));  
         return mix64(newSeed);  
     }  
228    
229      /**      /**
230       * Returns an initial default seed.       * Returns hash of local host IP address, if available; else 0.
231       */       */
232      private static long getInitialDefaultSeed() {      private static int hashedHostAddress() {
233          byte[] seedBytes = java.security.SecureRandom.getSeed(8);          try {
234          long s = (long)(seedBytes[0]) & 0xffL;              return InetAddress.getLocalHost().hashCode();
235          for (int i = 1; i < 8; ++i)          } catch (Exception ex) {
236              s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);              return 0;
         return s;  
237      }      }
238        }
239    
240        // IllegalArgumentException messages
241        static final String BadBound = "bound must be positive";
242        static final String BadRange = "bound must be greater than origin";
243        static final String BadSize  = "size must be non-negative";
244    
245      /*      /*
246       * Internal versions of nextX methods used by streams, as well as       * Internal versions of nextX methods used by streams, as well as
# Line 440  Line 361 
361       * @param seed the initial seed       * @param seed the initial seed
362       */       */
363      public SplittableRandom(long seed) {      public SplittableRandom(long seed) {
364          this(seed, 0L);          this(seed, INITIAL_GAMMA);
365      }      }
366    
367      /**      /**
# Line 449  Line 370 
370       * of those of any other instances in the current program; and       * of those of any other instances in the current program; and
371       * may, and typically does, vary across program invocations.       * may, and typically does, vary across program invocations.
372       */       */
373      public SplittableRandom() {      public SplittableRandom() { // emulate seeder.split()
374          this(nextDefaultSeed(), GAMMA_GAMMA);          this.gamma = nextGamma(this.seed = seeder.addAndGet(INITIAL_GAMMA));
375      }      }
376    
377      /**      /**
# Line 468  Line 389 
389       * @return the new SplittableRandom instance       * @return the new SplittableRandom instance
390       */       */
391      public SplittableRandom split() {      public SplittableRandom split() {
392          return new SplittableRandom(nextSeed(), nextSplit);          long s = nextSeed();
393            return new SplittableRandom(s, nextGamma(s));
394      }      }
395    
396      /**      /**
# Line 492  Line 414 
414       */       */
415      public int nextInt(int bound) {      public int nextInt(int bound) {
416          if (bound <= 0)          if (bound <= 0)
417              throw new IllegalArgumentException("bound must be positive");              throw new IllegalArgumentException(BadBound);
418          // Specialize internalNextInt for origin 0          // Specialize internalNextInt for origin 0
419          int r = mix32(nextSeed());          int r = mix32(nextSeed());
420          int m = bound - 1;          int m = bound - 1;
# Line 520  Line 442 
442       */       */
443      public int nextInt(int origin, int bound) {      public int nextInt(int origin, int bound) {
444          if (origin >= bound)          if (origin >= bound)
445              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
446          return internalNextInt(origin, bound);          return internalNextInt(origin, bound);
447      }      }
448    
# Line 545  Line 467 
467       */       */
468      public long nextLong(long bound) {      public long nextLong(long bound) {
469          if (bound <= 0)          if (bound <= 0)
470              throw new IllegalArgumentException("bound must be positive");              throw new IllegalArgumentException(BadBound);
471          // Specialize internalNextLong for origin 0          // Specialize internalNextLong for origin 0
472          long r = mix64(nextSeed());          long r = mix64(nextSeed());
473          long m = bound - 1;          long m = bound - 1;
# Line 573  Line 495 
495       */       */
496      public long nextLong(long origin, long bound) {      public long nextLong(long origin, long bound) {
497          if (origin >= bound)          if (origin >= bound)
498              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
499          return internalNextLong(origin, bound);          return internalNextLong(origin, bound);
500      }      }
501    
# Line 600  Line 522 
522       */       */
523      public double nextDouble(double bound) {      public double nextDouble(double bound) {
524          if (!(bound > 0.0))          if (!(bound > 0.0))
525              throw new IllegalArgumentException("bound must be positive");              throw new IllegalArgumentException(BadBound);
526          double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;          double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
527          return (result < bound) ?  result : // correct for rounding          return (result < bound) ?  result : // correct for rounding
528              Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);              Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
# Line 619  Line 541 
541       */       */
542      public double nextDouble(double origin, double bound) {      public double nextDouble(double origin, double bound) {
543          if (!(origin < bound))          if (!(origin < bound))
544              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
545          return internalNextDouble(origin, bound);          return internalNextDouble(origin, bound);
546      }      }
547    
# Line 646  Line 568 
568       */       */
569      public IntStream ints(long streamSize) {      public IntStream ints(long streamSize) {
570          if (streamSize < 0L)          if (streamSize < 0L)
571              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
572          return StreamSupport.intStream          return StreamSupport.intStream
573              (new RandomIntsSpliterator              (new RandomIntsSpliterator
574               (this, 0L, streamSize, Integer.MAX_VALUE, 0),               (this, 0L, streamSize, Integer.MAX_VALUE, 0),
# Line 686  Line 608 
608      public IntStream ints(long streamSize, int randomNumberOrigin,      public IntStream ints(long streamSize, int randomNumberOrigin,
609                            int randomNumberBound) {                            int randomNumberBound) {
610          if (streamSize < 0L)          if (streamSize < 0L)
611              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
612          if (randomNumberOrigin >= randomNumberBound)          if (randomNumberOrigin >= randomNumberBound)
613              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
614          return StreamSupport.intStream          return StreamSupport.intStream
615              (new RandomIntsSpliterator              (new RandomIntsSpliterator
616               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
# Line 711  Line 633 
633       */       */
634      public IntStream ints(int randomNumberOrigin, int randomNumberBound) {      public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
635          if (randomNumberOrigin >= randomNumberBound)          if (randomNumberOrigin >= randomNumberBound)
636              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
637          return StreamSupport.intStream          return StreamSupport.intStream
638              (new RandomIntsSpliterator              (new RandomIntsSpliterator
639               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
# Line 729  Line 651 
651       */       */
652      public LongStream longs(long streamSize) {      public LongStream longs(long streamSize) {
653          if (streamSize < 0L)          if (streamSize < 0L)
654              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
655          return StreamSupport.longStream          return StreamSupport.longStream
656              (new RandomLongsSpliterator              (new RandomLongsSpliterator
657               (this, 0L, streamSize, Long.MAX_VALUE, 0L),               (this, 0L, streamSize, Long.MAX_VALUE, 0L),
# Line 769  Line 691 
691      public LongStream longs(long streamSize, long randomNumberOrigin,      public LongStream longs(long streamSize, long randomNumberOrigin,
692                              long randomNumberBound) {                              long randomNumberBound) {
693          if (streamSize < 0L)          if (streamSize < 0L)
694              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
695          if (randomNumberOrigin >= randomNumberBound)          if (randomNumberOrigin >= randomNumberBound)
696              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
697          return StreamSupport.longStream          return StreamSupport.longStream
698              (new RandomLongsSpliterator              (new RandomLongsSpliterator
699               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
# Line 794  Line 716 
716       */       */
717      public LongStream longs(long randomNumberOrigin, long randomNumberBound) {      public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
718          if (randomNumberOrigin >= randomNumberBound)          if (randomNumberOrigin >= randomNumberBound)
719              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
720          return StreamSupport.longStream          return StreamSupport.longStream
721              (new RandomLongsSpliterator              (new RandomLongsSpliterator
722               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
# Line 813  Line 735 
735       */       */
736      public DoubleStream doubles(long streamSize) {      public DoubleStream doubles(long streamSize) {
737          if (streamSize < 0L)          if (streamSize < 0L)
738              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
739          return StreamSupport.doubleStream          return StreamSupport.doubleStream
740              (new RandomDoublesSpliterator              (new RandomDoublesSpliterator
741               (this, 0L, streamSize, Double.MAX_VALUE, 0.0),               (this, 0L, streamSize, Double.MAX_VALUE, 0.0),
# Line 855  Line 777 
777      public DoubleStream doubles(long streamSize, double randomNumberOrigin,      public DoubleStream doubles(long streamSize, double randomNumberOrigin,
778                                  double randomNumberBound) {                                  double randomNumberBound) {
779          if (streamSize < 0L)          if (streamSize < 0L)
780              throw new IllegalArgumentException("negative Stream size");              throw new IllegalArgumentException(BadSize);
781          if (!(randomNumberOrigin < randomNumberBound))          if (!(randomNumberOrigin < randomNumberBound))
782              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
783          return StreamSupport.doubleStream          return StreamSupport.doubleStream
784              (new RandomDoublesSpliterator              (new RandomDoublesSpliterator
785               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),               (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
# Line 880  Line 802 
802       */       */
803      public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {      public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
804          if (!(randomNumberOrigin < randomNumberBound))          if (!(randomNumberOrigin < randomNumberBound))
805              throw new IllegalArgumentException("bound must be greater than origin");              throw new IllegalArgumentException(BadRange);
806          return StreamSupport.doubleStream          return StreamSupport.doubleStream
807              (new RandomDoublesSpliterator              (new RandomDoublesSpliterator
808               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),               (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
# Line 938  Line 860 
860              long i = index, f = fence;              long i = index, f = fence;
861              if (i < f) {              if (i < f) {
862                  index = f;                  index = f;
863                    SplittableRandom r = rng;
864                  int o = origin, b = bound;                  int o = origin, b = bound;
865                  do {                  do {
866                      consumer.accept(rng.internalNextInt(o, b));                      consumer.accept(r.internalNextInt(o, b));
867                  } while (++i < f);                  } while (++i < f);
868              }              }
869          }          }
# Line 992  Line 915 
915              long i = index, f = fence;              long i = index, f = fence;
916              if (i < f) {              if (i < f) {
917                  index = f;                  index = f;
918                    SplittableRandom r = rng;
919                  long o = origin, b = bound;                  long o = origin, b = bound;
920                  do {                  do {
921                      consumer.accept(rng.internalNextLong(o, b));                      consumer.accept(r.internalNextLong(o, b));
922                  } while (++i < f);                  } while (++i < f);
923              }              }
924          }          }
# Line 1047  Line 971 
971              long i = index, f = fence;              long i = index, f = fence;
972              if (i < f) {              if (i < f) {
973                  index = f;                  index = f;
974                    SplittableRandom r = rng;
975                  double o = origin, b = bound;                  double o = origin, b = bound;
976                  do {                  do {
977                      consumer.accept(rng.internalNextDouble(o, b));                      consumer.accept(r.internalNextDouble(o, b));
978                  } while (++i < f);                  } while (++i < f);
979              }              }
980          }          }

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8