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.1 by dl, Wed Jul 10 15:40:19 2013 UTC vs.
Revision 1.6 by dl, Thu Jul 11 23:22:01 2013 UTC

# Line 40 | Line 40 | import java.util.stream.DoubleStream;
40   * A generator of uniform pseudorandom values applicable for use in
41   * (among other contexts) isolated parallel computations that may
42   * generate subtasks. Class SplittableRandom supports methods for
43 < * producing pseudorandom nunmbers of type {@code int}, {@code long},
43 > * producing pseudorandom numbers of type {@code int}, {@code long},
44   * and {@code double} with similar usages as for class
45   * {@link java.util.Random} but differs in the following ways: <ul>
46   *
# Line 74 | Line 74 | import java.util.stream.DoubleStream;
74   * </ul>
75   *
76   * @author  Guy Steele
77 + * @author  Doug Lea
78   * @since   1.8
79   */
80   public class SplittableRandom {
# Line 112 | Line 113 | public class SplittableRandom {
113       * The value of gamma differs for each instance across a series of
114       * splits, and is generated using a slightly stripped-down variant
115       * of the same algorithm, but operating across calls to split(),
116 <     * not calls to nextLong(): Each instance carries the state of
116 >     * not calls to nextSeed(): Each instance carries the state of
117       * this generator as nextSplit, and uses mix64(nextSplit) as its
118       * own gamma value. Computations of gammas themselves use a fixed
119       * constant as the second argument to the addGammaModGeorge
# Line 177 | Line 178 | public class SplittableRandom {
178      private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L;
179  
180      /**
181 +     * The least non-zero value returned by nextDouble(). This value
182 +     * is scaled by a random value of 52 bits to produce a result.
183 +     */
184 +    private static final double DOUBLE_UNIT = 1.0 / (1L << 53);
185 +
186 +    /**
187       * The next seed for default constructors.
188       */
189      private static final AtomicLong defaultSeedGenerator =
# Line 310 | Line 317 | public class SplittableRandom {
317           * evenly divisible by the range. The loop rejects candidates
318           * computed from otherwise over-represented values.  The
319           * expected number of iterations under an ideal generator
320 <         * varies from 1 to 2, depending on the bound.
320 >         * varies from 1 to 2, depending on the bound. The loop itself
321 >         * takes an unlovable form. Because the first candidate is
322 >         * already available, we need a break-in-the-middle
323 >         * construction, which is concisely but cryptically performed
324 >         * within the while-condition of a body-less for loop.
325           *
326           * 4. Otherwise, the range cannot be represented as a positive
327 <         * long.  Repeatedly generate unbounded longs until obtaining
328 <         * a candidate meeting constraints (with an expected number of
329 <         * iterations of less than two).
327 >         * long.  The loop repeatedly generates unbounded longs until
328 >         * obtaining a candidate meeting constraints (with an expected
329 >         * number of iterations of less than two).
330           */
331  
332          long r = mix64(nextSeed());
# Line 375 | Line 386 | public class SplittableRandom {
386       * @return a pseudorandom value
387       */
388      final double internalNextDouble(double origin, double bound) {
389 <        long bits = (1023L << 52) | (nextLong() >>> 12);
379 <        double r = Double.longBitsToDouble(bits) - 1.0;
389 >        double r = (nextLong() >>> 11) * DOUBLE_UNIT;
390          if (origin < bound) {
391              r = r * (bound - origin) + origin;
392              if (r == bound) // correct for rounding
# Line 540 | Line 550 | public class SplittableRandom {
550       * (inclusive) and {@code 1.0} (exclusive)
551       */
552      public double nextDouble() {
553 <        long bits = (1023L << 52) | (nextLong() >>> 12);
544 <        return Double.longBitsToDouble(bits) - 1.0;
553 >        return (nextLong() >>> 11) * DOUBLE_UNIT;
554      }
555  
556      /**
# Line 752 | Line 761 | public class SplittableRandom {
761  
762      /**
763       * Returns a stream with the given {@code streamSize} number of
764 <     * pseudorandom {@code double} values.
764 >     * pseudorandom {@code double} values, each between {@code 0.0}
765 >     * (inclusive) and {@code 1.0} (exclusive).
766       *
767       * @param streamSize the number of values to generate
768       * @return a stream of {@code double} values
# Line 770 | Line 780 | public class SplittableRandom {
780  
781      /**
782       * Returns an effectively unlimited stream of pseudorandom {@code
783 <     * double} values.
783 >     * double} values, each between {@code 0.0} (inclusive) and {@code
784 >     * 1.0} (exclusive).
785       *
786       * @implNote This method is implemented to be equivalent to {@code
787       * doubles(Long.MAX_VALUE)}.
# Line 866 | Line 877 | public class SplittableRandom {
877  
878          public int characteristics() {
879              return (Spliterator.SIZED | Spliterator.SUBSIZED |
880 <                    Spliterator.ORDERED | Spliterator.NONNULL |
870 <                    Spliterator.IMMUTABLE);
880 >                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
881          }
882  
883          public boolean tryAdvance(IntConsumer consumer) {
# Line 921 | Line 931 | public class SplittableRandom {
931  
932          public int characteristics() {
933              return (Spliterator.SIZED | Spliterator.SUBSIZED |
934 <                    Spliterator.ORDERED | Spliterator.NONNULL |
925 <                    Spliterator.IMMUTABLE);
934 >                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
935          }
936  
937          public boolean tryAdvance(LongConsumer consumer) {
# Line 977 | Line 986 | public class SplittableRandom {
986  
987          public int characteristics() {
988              return (Spliterator.SIZED | Spliterator.SUBSIZED |
989 <                    Spliterator.ORDERED | Spliterator.NONNULL |
981 <                    Spliterator.IMMUTABLE);
989 >                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
990          }
991  
992          public boolean tryAdvance(DoubleConsumer consumer) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines