[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.11, Tue Jul 16 12:32:05 2013 UTC revision 1.12, Sun Jul 21 14:02:23 2013 UTC
# Line 25  Line 25 
25    
26  package java.util;  package java.util;
27    
28    import java.security.SecureRandom;
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 121  Line 122 
122       * this generator as nextSplit, and uses mix64(nextSplit) as its       * this generator as nextSplit, and uses mix64(nextSplit) as its
123       * own gamma value. Computations of gammas themselves use a fixed       * own gamma value. Computations of gammas themselves use a fixed
124       * constant as the second argument to the addGammaModGeorge       * constant as the second argument to the addGammaModGeorge
125       * function, GAMMA_GAMMA, a "genuinely random" number from a       * function, GAMMA_GAMMA. The value of GAMMA_GAMMA is arbitrary
126       * radioactive decay reading (obtained from       * (except must be at least 13), but because it serves as the base
127       * http://www.fourmilab.ch/hotbits/) meeting the above range       * of split sequences, should be subject to validation of
128       * constraint. Using a fixed constant maintains the invariant that       * consequent random number quality metrics.
      * the value of gamma is the same for every instance that is at  
      * the same split-distance from their common root. (Note: there is  
      * nothing especially magic about obtaining this constant from a  
      * "truly random" physical source rather than just choosing one  
      * arbitrarily; using "hotbits" was merely an aesthetically pleasing  
      * choice.  In either case, good statistical behavior of the  
      * algorithm should be, and was, verified by using the DieHarder  
      * test suite.)  
129       *       *
130       * The mix64 bit-mixing function called by nextLong and other       * The mix64 bit-mixing function called by nextLong and other
131       * methods computes the same value as the "64-bit finalizer"       * methods computes the same value as the "64-bit finalizer"
# Line 158  Line 151 
151       * SplittableRandom. Unlike other cases, this split must be       * SplittableRandom. Unlike other cases, this split must be
152       * performed in a thread-safe manner. We use       * performed in a thread-safe manner. We use
153       * AtomicLong.compareAndSet as the (typically) most efficient       * AtomicLong.compareAndSet as the (typically) most efficient
154       * mechanism. To bootstrap, we start off using a function of the       * mechanism. To bootstrap, we start off using a SecureRandom
155       * current System time as seed, and update using another       * initial default seed, and update using a fixed
156       * "genuinely random" constant DEFAULT_SEED_GAMMA. The default       * DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA,
157       * constructor uses GAMMA_GAMMA, not 0, for its splitSeed argument       * not 0, for its splitSeed argument (addGammaModGeorge(0,
158       * (addGammaModGeorge(0, GAMMA_GAMMA) == GAMMA_GAMMA) to reflect       * GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from
159       * that each is split from this root generator, even though the       * this root generator, even though the root is not explicitly
160       * root is not explicitly represented as a SplittableRandom.  When       * represented as a SplittableRandom.
      * establishing the initial seed, we use both  
      * System.currentTimeMillis and System.nanoTime(), to avoid  
      * regularities that may occur if using either alone.  
161       */       */
162    
163      /**      /**
164       * The "genuinely random" value for producing new gamma values.       * The value for producing new gamma values. Must be greater or
165       * The value is arbitrary, subject to the requirement that it be       * equal to 13. Otherwise, the value is arbitrary subject to
166       * greater or equal to 13.       * validation of the resulting statistical quality of splits.
167       */       */
168      private static final long GAMMA_GAMMA = 0xF2281E2DBA6606F3L;      private static final long GAMMA_GAMMA = 0xF2281E2DBA6606F3L;
169    
170      /**      /**
171       * The "genuinely random" seed update value for default constructors.       * The seed update value for default constructors.  Must be
172       * The value is arbitrary, subject to the requirement that it be       * greater or equal to 13. Otherwise, the value is arbitrary.
      * greater or equal to 13.  
173       */       */
174      private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L;      private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L;
175    
# Line 200  Line 189 
189       * The next seed for default constructors.       * The next seed for default constructors.
190       */       */
191      private static final AtomicLong defaultSeedGenerator =      private static final AtomicLong defaultSeedGenerator =
192          new AtomicLong(mix64(System.currentTimeMillis()) ^          new AtomicLong(getInitialDefaultSeed());
                        mix64(System.nanoTime()));  
193    
194      /**      /**
195       * The seed, updated only via method nextSeed.       * The seed, updated only via method nextSeed.
# Line 302  Line 290 
290          return mix64(newSeed);          return mix64(newSeed);
291      }      }
292    
293        /**
294         * Returns an initial default seed.
295         */
296        private static long getInitialDefaultSeed() {
297            byte[] seedBytes = java.security.SecureRandom.getSeed(8);
298            long s = (long)(seedBytes[0]) & 0xffL;
299            for (int i = 1; i < 8; ++i)
300                s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
301            return s;
302        }
303    
304      /*      /*
305       * Internal versions of nextX methods used by streams, as well as       * Internal versions of nextX methods used by streams, as well as
306       * the public nextX(origin, bound) methods.  These exist mainly to       * the public nextX(origin, bound) methods.  These exist mainly to

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8