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.22 by jsr166, Sun May 18 23:59:57 2008 UTC

# Line 51 | Line 51 | import sun.misc.Unsafe;
51   * Many applications will find the method {@link Math#random} simpler to use.
52   *
53   * @author  Frank Yellin
54 * @version %I%, %G%
54   * @since   1.0
55   */
56   public
# Line 118 | Line 117 | class Random implements java.io.Serializ
117      synchronized public void setSeed(long seed) {
118          seed = (seed ^ multiplier) & mask;
119          this.seed.set(seed);
120 <        haveNextNextGaussian = false;
120 >        haveNextNextGaussian = false;
121      }
122  
123      /**
# Line 150 | Line 149 | class Random implements java.io.Serializ
149          long oldseed, nextseed;
150          AtomicLong seed = this.seed;
151          do {
152 <            oldseed = seed.get();
153 <            nextseed = (oldseed * multiplier + addend) & mask;
152 >            oldseed = seed.get();
153 >            nextseed = (oldseed * multiplier + addend) & mask;
154          } while (!seed.compareAndSet(oldseed, nextseed));
155          return (int)(nextseed >>> (48 - bits));
156      }
# Line 176 | Line 175 | class Random implements java.io.Serializ
175       * @since  1.1
176       */
177      public void nextBytes(byte[] bytes) {
178 <        for (int i = 0, len = bytes.length; i < len; )
179 <            for (int rnd = nextInt(),
180 <                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
181 <                 n-- > 0; rnd >>= Byte.SIZE)
182 <                bytes[i++] = (byte)rnd;
178 >        for (int i = 0, len = bytes.length; i < len; )
179 >            for (int rnd = nextInt(),
180 >                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
181 >                 n-- > 0; rnd >>= Byte.SIZE)
182 >                bytes[i++] = (byte)rnd;
183      }
184  
185      /**
# Line 202 | Line 201 | class Random implements java.io.Serializ
201       *         value from this random number generator's sequence
202       */
203      public int nextInt() {
204 <        return next(32);
204 >        return next(32);
205      }
206  
207      /**
# Line 253 | Line 252 | class Random implements java.io.Serializ
252       * successive calls to this method if n is a small power of two.
253       *
254       * @param n the bound on the random number to be returned.  Must be
255 <     *        positive.
255 >     *        positive.
256       * @return the next pseudorandom, uniformly distributed {@code int}
257       *         value between {@code 0} (inclusive) and {@code n} (exclusive)
258       *         from this random number generator's sequence
# Line 317 | Line 316 | class Random implements java.io.Serializ
316       *
317       * @return the next pseudorandom, uniformly distributed
318       *         {@code boolean} value from this random number generator's
319 <     *         sequence
319 >     *         sequence
320       * @since 1.2
321       */
322      public boolean nextBoolean() {
323 <        return next(1) != 0;
323 >        return next(1) != 0;
324      }
325  
326      /**
# Line 408 | Line 407 | class Random implements java.io.Serializ
407       */
408      public double nextDouble() {
409          return (((long)(next(26)) << 27) + next(27))
410 <            / (double)(1L << 53);
410 >            / (double)(1L << 53);
411      }
412  
413      private double nextNextGaussian;
# Line 462 | Line 461 | class Random implements java.io.Serializ
461      synchronized public double nextGaussian() {
462          // See Knuth, ACP, Section 3.4.1 Algorithm C.
463          if (haveNextNextGaussian) {
464 <            haveNextNextGaussian = false;
465 <            return nextNextGaussian;
466 <        } else {
464 >            haveNextNextGaussian = false;
465 >            return nextNextGaussian;
466 >        } else {
467              double v1, v2, s;
468 <            do {
468 >            do {
469                  v1 = 2 * nextDouble() - 1; // between -1 and 1
470 <                v2 = 2 * nextDouble() - 1; // between -1 and 1
470 >                v2 = 2 * nextDouble() - 1; // between -1 and 1
471                  s = v1 * v1 + v2 * v2;
472 <            } while (s >= 1 || s == 0);
473 <            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
474 <            nextNextGaussian = v2 * multiplier;
475 <            haveNextNextGaussian = true;
476 <            return v1 * multiplier;
472 >            } while (s >= 1 || s == 0);
473 >            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
474 >            nextNextGaussian = v2 * multiplier;
475 >            haveNextNextGaussian = true;
476 >            return v1 * multiplier;
477          }
478      }
479  
# Line 503 | Line 502 | class Random implements java.io.Serializ
502  
503          ObjectInputStream.GetField fields = s.readFields();
504  
505 <        // The seed is read in as {@code long} for
506 <        // historical reasons, but it is converted to an AtomicLong.
507 <        long seedVal = (long) fields.get("seed", -1L);
505 >        // The seed is read in as {@code long} for
506 >        // historical reasons, but it is converted to an AtomicLong.
507 >        long seedVal = (long) fields.get("seed", -1L);
508          if (seedVal < 0)
509            throw new java.io.StreamCorruptedException(
510                                "Random: invalid seed");
# Line 518 | Line 517 | class Random implements java.io.Serializ
517       * Save the {@code Random} instance to a stream.
518       */
519      synchronized private void writeObject(ObjectOutputStream s)
520 <        throws IOException {
520 >        throws IOException {
521  
522          // set the values of the Serializable fields
523          ObjectOutputStream.PutField fields = s.putFields();
524  
525 <        // The seed is serialized as a long for historical reasons.
525 >        // The seed is serialized as a long for historical reasons.
526          fields.put("seed", seed.get());
527          fields.put("nextNextGaussian", nextNextGaussian);
528          fields.put("haveNextNextGaussian", haveNextNextGaussian);
# Line 539 | Line 538 | class Random implements java.io.Serializ
538          try {
539              seedOffset = unsafe.objectFieldOffset
540                  (Random.class.getDeclaredField("seed"));
541 <        } catch (Exception ex) { throw new Error(ex); }
541 >        } catch (Exception ex) { throw new Error(ex); }
542      }
543      private void resetSeed(long seedVal) {
544          unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines