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.3 by jsr166, Thu Jul 11 03:31:26 2013 UTC vs.
Revision 1.5 by dl, Thu Jul 11 23:14:45 2013 UTC

# Line 178 | 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 53 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 311 | 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 376 | 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);
380 <        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 541 | Line 550 | public class SplittableRandom {
550       * (inclusive) and {@code 1.0} (exclusive)
551       */
552      public double nextDouble() {
553 <        long bits = (1023L << 52) | (nextLong() >>> 12);
545 <        return Double.longBitsToDouble(bits) - 1.0;
553 >        return (nextLong() >>> 11) * DOUBLE_UNIT;
554      }
555  
556      /**
# Line 869 | Line 877 | public class SplittableRandom {
877  
878          public int characteristics() {
879              return (Spliterator.SIZED | Spliterator.SUBSIZED |
880 <                    Spliterator.ORDERED | Spliterator.NONNULL |
873 <                    Spliterator.IMMUTABLE);
880 >                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
881          }
882  
883          public boolean tryAdvance(IntConsumer consumer) {
# Line 924 | Line 931 | public class SplittableRandom {
931  
932          public int characteristics() {
933              return (Spliterator.SIZED | Spliterator.SUBSIZED |
934 <                    Spliterator.ORDERED | Spliterator.NONNULL |
928 <                    Spliterator.IMMUTABLE);
934 >                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
935          }
936  
937          public boolean tryAdvance(LongConsumer consumer) {
# Line 980 | Line 986 | public class SplittableRandom {
986  
987          public int characteristics() {
988              return (Spliterator.SIZED | Spliterator.SUBSIZED |
989 <                    Spliterator.ORDERED | Spliterator.NONNULL |
984 <                    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