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

Print this page




  60  * <p>Instances of {@code java.util.Random} are not cryptographically
  61  * secure.  Consider instead using {@link java.security.SecureRandom} to
  62  * get a cryptographically secure pseudo-random number generator for use
  63  * by security-sensitive applications.
  64  *
  65  * @author  Frank Yellin
  66  * @since   1.0
  67  */
  68 public
  69 class Random implements java.io.Serializable {
  70     /** use serialVersionUID from JDK 1.1 for interoperability */
  71     static final long serialVersionUID = 3905348978240129619L;
  72 
  73     /**
  74      * The internal state associated with this pseudorandom number generator.
  75      * (The specs for the methods in this class describe the ongoing
  76      * computation of this value.)
  77      */
  78     private final AtomicLong seed;
  79 
  80     private final static long multiplier = 0x5DEECE66DL;
  81     private final static long addend = 0xBL;
  82     private final static long mask = (1L << 48) - 1;
  83 
  84     /**
  85      * Creates a new random number generator. This constructor sets
  86      * the seed of the random number generator to a value very likely
  87      * to be distinct from any other invocation of this constructor.
  88      */
  89     public Random() {
  90         this(seedUniquifier() ^ System.nanoTime());
  91     }
  92 
  93     private static long seedUniquifier() {
  94         // L'Ecuyer, "Tables of Linear Congruential Generators of
  95         // Different Sizes and Good Lattice Structure", 1999
  96         for (;;) {
  97             long current = seedUniquifier.get();
  98             long next = current * 181783497276652981L;
  99             if (seedUniquifier.compareAndSet(current, next))
 100                 return next;
 101         }
 102     }


 268      * in an uneven distribution (due to the fact that 2^31 is not divisible
 269      * by n). The probability of a value being rejected depends on n.  The
 270      * worst case is n=2^30+1, for which the probability of a reject is 1/2,
 271      * and the expected number of iterations before the loop terminates is 2.
 272      * <p>
 273      * The algorithm treats the case where n is a power of two specially: it
 274      * returns the correct number of high-order bits from the underlying
 275      * pseudo-random number generator.  In the absence of special treatment,
 276      * the correct number of <i>low-order</i> bits would be returned.  Linear
 277      * congruential pseudo-random number generators such as the one
 278      * implemented by this class are known to have short periods in the
 279      * sequence of values of their low-order bits.  Thus, this special case
 280      * greatly increases the length of the sequence of values returned by
 281      * successive calls to this method if n is a small power of two.
 282      *
 283      * @param n the bound on the random number to be returned.  Must be
 284      *        positive.
 285      * @return the next pseudorandom, uniformly distributed {@code int}
 286      *         value between {@code 0} (inclusive) and {@code n} (exclusive)
 287      *         from this random number generator's sequence
 288      * @exception IllegalArgumentException if n is not positive
 289      * @since 1.2
 290      */
 291 
 292     public int nextInt(int n) {
 293         if (n <= 0)
 294             throw new IllegalArgumentException("n must be positive");
 295 
 296         if ((n & -n) == n)  // i.e., n is a power of 2
 297             return (int)((n * (long)next(31)) >> 31);
 298 
 299         int bits, val;
 300         do {
 301             bits = next(31);
 302             val = bits % n;
 303         } while (bits - val + (n-1) < 0);
 304         return val;
 305     }
 306 
 307     /**
 308      * Returns the next pseudorandom, uniformly distributed {@code long}




  60  * <p>Instances of {@code java.util.Random} are not cryptographically
  61  * secure.  Consider instead using {@link java.security.SecureRandom} to
  62  * get a cryptographically secure pseudo-random number generator for use
  63  * by security-sensitive applications.
  64  *
  65  * @author  Frank Yellin
  66  * @since   1.0
  67  */
  68 public
  69 class Random implements java.io.Serializable {
  70     /** use serialVersionUID from JDK 1.1 for interoperability */
  71     static final long serialVersionUID = 3905348978240129619L;
  72 
  73     /**
  74      * The internal state associated with this pseudorandom number generator.
  75      * (The specs for the methods in this class describe the ongoing
  76      * computation of this value.)
  77      */
  78     private final AtomicLong seed;
  79 
  80     private static final long multiplier = 0x5DEECE66DL;
  81     private static final long addend = 0xBL;
  82     private static final long mask = (1L << 48) - 1;
  83 
  84     /**
  85      * Creates a new random number generator. This constructor sets
  86      * the seed of the random number generator to a value very likely
  87      * to be distinct from any other invocation of this constructor.
  88      */
  89     public Random() {
  90         this(seedUniquifier() ^ System.nanoTime());
  91     }
  92 
  93     private static long seedUniquifier() {
  94         // L'Ecuyer, "Tables of Linear Congruential Generators of
  95         // Different Sizes and Good Lattice Structure", 1999
  96         for (;;) {
  97             long current = seedUniquifier.get();
  98             long next = current * 181783497276652981L;
  99             if (seedUniquifier.compareAndSet(current, next))
 100                 return next;
 101         }
 102     }


 268      * in an uneven distribution (due to the fact that 2^31 is not divisible
 269      * by n). The probability of a value being rejected depends on n.  The
 270      * worst case is n=2^30+1, for which the probability of a reject is 1/2,
 271      * and the expected number of iterations before the loop terminates is 2.
 272      * <p>
 273      * The algorithm treats the case where n is a power of two specially: it
 274      * returns the correct number of high-order bits from the underlying
 275      * pseudo-random number generator.  In the absence of special treatment,
 276      * the correct number of <i>low-order</i> bits would be returned.  Linear
 277      * congruential pseudo-random number generators such as the one
 278      * implemented by this class are known to have short periods in the
 279      * sequence of values of their low-order bits.  Thus, this special case
 280      * greatly increases the length of the sequence of values returned by
 281      * successive calls to this method if n is a small power of two.
 282      *
 283      * @param n the bound on the random number to be returned.  Must be
 284      *        positive.
 285      * @return the next pseudorandom, uniformly distributed {@code int}
 286      *         value between {@code 0} (inclusive) and {@code n} (exclusive)
 287      *         from this random number generator's sequence
 288      * @throws IllegalArgumentException if n is not positive
 289      * @since 1.2
 290      */
 291 
 292     public int nextInt(int n) {
 293         if (n <= 0)
 294             throw new IllegalArgumentException("n must be positive");
 295 
 296         if ((n & -n) == n)  // i.e., n is a power of 2
 297             return (int)((n * (long)next(31)) >> 31);
 298 
 299         int bits, val;
 300         do {
 301             bits = next(31);
 302             val = bits % n;
 303         } while (bits - val + (n-1) < 0);
 304         return val;
 305     }
 306 
 307     /**
 308      * Returns the next pseudorandom, uniformly distributed {@code long}