< prev index next >

src/java.base/share/classes/java/util/Random.java

Print this page
8201634: Random seedUniquifier uses incorrect LCG
Summary: Correct typo in constant from L'Ecuyer
Reviewed-by: dl, psandoz


  93 
  94     // IllegalArgumentException messages
  95     static final String BadBound = "bound must be positive";
  96     static final String BadRange = "bound must be greater than origin";
  97     static final String BadSize  = "size must be non-negative";
  98 
  99     /**
 100      * Creates a new random number generator. This constructor sets
 101      * the seed of the random number generator to a value very likely
 102      * to be distinct from any other invocation of this constructor.
 103      */
 104     public Random() {
 105         this(seedUniquifier() ^ System.nanoTime());
 106     }
 107 
 108     private static long seedUniquifier() {
 109         // L'Ecuyer, "Tables of Linear Congruential Generators of
 110         // Different Sizes and Good Lattice Structure", 1999
 111         for (;;) {
 112             long current = seedUniquifier.get();
 113             long next = current * 181783497276652981L;
 114             if (seedUniquifier.compareAndSet(current, next))
 115                 return next;
 116         }
 117     }
 118 
 119     private static final AtomicLong seedUniquifier
 120         = new AtomicLong(8682522807148012L);
 121 
 122     /**
 123      * Creates a new random number generator using a single {@code long} seed.
 124      * The seed is the initial value of the internal state of the pseudorandom
 125      * number generator which is maintained by method {@link #next}.
 126      *
 127      * <p>The invocation {@code new Random(seed)} is equivalent to:
 128      *  <pre> {@code
 129      * Random rnd = new Random();
 130      * rnd.setSeed(seed);}</pre>
 131      *
 132      * @param seed the initial seed
 133      * @see   #setSeed(long)




  93 
  94     // IllegalArgumentException messages
  95     static final String BadBound = "bound must be positive";
  96     static final String BadRange = "bound must be greater than origin";
  97     static final String BadSize  = "size must be non-negative";
  98 
  99     /**
 100      * Creates a new random number generator. This constructor sets
 101      * the seed of the random number generator to a value very likely
 102      * to be distinct from any other invocation of this constructor.
 103      */
 104     public Random() {
 105         this(seedUniquifier() ^ System.nanoTime());
 106     }
 107 
 108     private static long seedUniquifier() {
 109         // L'Ecuyer, "Tables of Linear Congruential Generators of
 110         // Different Sizes and Good Lattice Structure", 1999
 111         for (;;) {
 112             long current = seedUniquifier.get();
 113             long next = current * 1181783497276652981L;
 114             if (seedUniquifier.compareAndSet(current, next))
 115                 return next;
 116         }
 117     }
 118 
 119     private static final AtomicLong seedUniquifier
 120         = new AtomicLong(8682522807148012L);
 121 
 122     /**
 123      * Creates a new random number generator using a single {@code long} seed.
 124      * The seed is the initial value of the internal state of the pseudorandom
 125      * number generator which is maintained by method {@link #next}.
 126      *
 127      * <p>The invocation {@code new Random(seed)} is equivalent to:
 128      *  <pre> {@code
 129      * Random rnd = new Random();
 130      * rnd.setSeed(seed);}</pre>
 131      *
 132      * @param seed the initial seed
 133      * @see   #setSeed(long)


< prev index next >