ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Random.java
(Generate patch)

Comparing jsr166/src/main/java/util/Random.java (file contents):
Revision 1.20 by jsr166, Sun May 20 07:54:01 2007 UTC vs.
Revision 1.25 by jsr166, Wed Aug 5 01:34:23 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 1995-2007 Sun Microsystems, Inc.  All Rights Reserved.
2 > * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 32 | Line 32 | import sun.misc.Unsafe;
32   * An instance of this class is used to generate a stream of
33   * pseudorandom numbers. The class uses a 48-bit seed, which is
34   * modified using a linear congruential formula. (See Donald Knuth,
35 < * <i>The Art of Computer Programming, Volume 3</i>, Section 3.2.1.)
35 > * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
36   * <p>
37   * If two instances of {@code Random} are created with the same
38   * seed, and the same sequence of method calls is made for each, they
# Line 50 | Line 50 | import sun.misc.Unsafe;
50   * <p>
51   * Many applications will find the method {@link Math#random} simpler to use.
52   *
53 + * <p>Instances of {@code java.util.Random} are threadsafe.
54 + * However, the concurrent use of the same {@code java.util.Random}
55 + * instance across threads may encounter contention and consequent
56 + * poor performance. Consider instead using
57 + * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
58 + * designs.
59 + *
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
54 * @version %I%, %G%
66   * @since   1.0
67   */
68   public
# Line 118 | Line 129 | class Random implements java.io.Serializ
129      synchronized public void setSeed(long seed) {
130          seed = (seed ^ multiplier) & mask;
131          this.seed.set(seed);
132 <        haveNextNextGaussian = false;
132 >        haveNextNextGaussian = false;
133      }
134  
135      /**
# Line 150 | Line 161 | class Random implements java.io.Serializ
161          long oldseed, nextseed;
162          AtomicLong seed = this.seed;
163          do {
164 <            oldseed = seed.get();
165 <            nextseed = (oldseed * multiplier + addend) & mask;
164 >            oldseed = seed.get();
165 >            nextseed = (oldseed * multiplier + addend) & mask;
166          } while (!seed.compareAndSet(oldseed, nextseed));
167          return (int)(nextseed >>> (48 - bits));
168      }
# Line 176 | Line 187 | class Random implements java.io.Serializ
187       * @since  1.1
188       */
189      public void nextBytes(byte[] bytes) {
190 <        for (int i = 0, len = bytes.length; i < len; )
191 <            for (int rnd = nextInt(),
192 <                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
193 <                 n-- > 0; rnd >>= Byte.SIZE)
194 <                bytes[i++] = (byte)rnd;
190 >        for (int i = 0, len = bytes.length; i < len; )
191 >            for (int rnd = nextInt(),
192 >                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
193 >                 n-- > 0; rnd >>= Byte.SIZE)
194 >                bytes[i++] = (byte)rnd;
195      }
196  
197      /**
# Line 202 | Line 213 | class Random implements java.io.Serializ
213       *         value from this random number generator's sequence
214       */
215      public int nextInt() {
216 <        return next(32);
216 >        return next(32);
217      }
218  
219      /**
# Line 253 | Line 264 | class Random implements java.io.Serializ
264       * successive calls to this method if n is a small power of two.
265       *
266       * @param n the bound on the random number to be returned.  Must be
267 <     *        positive.
267 >     *        positive.
268       * @return the next pseudorandom, uniformly distributed {@code int}
269       *         value between {@code 0} (inclusive) and {@code n} (exclusive)
270       *         from this random number generator's sequence
# Line 317 | Line 328 | class Random implements java.io.Serializ
328       *
329       * @return the next pseudorandom, uniformly distributed
330       *         {@code boolean} value from this random number generator's
331 <     *         sequence
331 >     *         sequence
332       * @since 1.2
333       */
334      public boolean nextBoolean() {
335 <        return next(1) != 0;
335 >        return next(1) != 0;
336      }
337  
338      /**
# Line 408 | Line 419 | class Random implements java.io.Serializ
419       */
420      public double nextDouble() {
421          return (((long)(next(26)) << 27) + next(27))
422 <            / (double)(1L << 53);
422 >            / (double)(1L << 53);
423      }
424  
425      private double nextNextGaussian;
# Line 462 | Line 473 | class Random implements java.io.Serializ
473      synchronized public double nextGaussian() {
474          // See Knuth, ACP, Section 3.4.1 Algorithm C.
475          if (haveNextNextGaussian) {
476 <            haveNextNextGaussian = false;
477 <            return nextNextGaussian;
478 <        } else {
476 >            haveNextNextGaussian = false;
477 >            return nextNextGaussian;
478 >        } else {
479              double v1, v2, s;
480 <            do {
480 >            do {
481                  v1 = 2 * nextDouble() - 1; // between -1 and 1
482 <                v2 = 2 * nextDouble() - 1; // between -1 and 1
482 >                v2 = 2 * nextDouble() - 1; // between -1 and 1
483                  s = v1 * v1 + v2 * v2;
484 <            } while (s >= 1 || s == 0);
485 <            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
486 <            nextNextGaussian = v2 * multiplier;
487 <            haveNextNextGaussian = true;
488 <            return v1 * multiplier;
484 >            } while (s >= 1 || s == 0);
485 >            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
486 >            nextNextGaussian = v2 * multiplier;
487 >            haveNextNextGaussian = true;
488 >            return v1 * multiplier;
489          }
490      }
491  
# Line 503 | Line 514 | class Random implements java.io.Serializ
514  
515          ObjectInputStream.GetField fields = s.readFields();
516  
517 <        // The seed is read in as {@code long} for
518 <        // historical reasons, but it is converted to an AtomicLong.
519 <        long seedVal = (long) fields.get("seed", -1L);
517 >        // The seed is read in as {@code long} for
518 >        // historical reasons, but it is converted to an AtomicLong.
519 >        long seedVal = fields.get("seed", -1L);
520          if (seedVal < 0)
521            throw new java.io.StreamCorruptedException(
522                                "Random: invalid seed");
# Line 518 | Line 529 | class Random implements java.io.Serializ
529       * Save the {@code Random} instance to a stream.
530       */
531      synchronized private void writeObject(ObjectOutputStream s)
532 <        throws IOException {
532 >        throws IOException {
533  
534          // set the values of the Serializable fields
535          ObjectOutputStream.PutField fields = s.putFields();
536  
537 <        // The seed is serialized as a long for historical reasons.
537 >        // The seed is serialized as a long for historical reasons.
538          fields.put("seed", seed.get());
539          fields.put("nextNextGaussian", nextNextGaussian);
540          fields.put("haveNextNextGaussian", haveNextNextGaussian);
# Line 539 | Line 550 | class Random implements java.io.Serializ
550          try {
551              seedOffset = unsafe.objectFieldOffset
552                  (Random.class.getDeclaredField("seed"));
553 <        } catch (Exception ex) { throw new Error(ex); }
553 >        } catch (Exception ex) { throw new Error(ex); }
554      }
555      private void resetSeed(long seedVal) {
556          unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines