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.26 by jsr166, Sun Sep 5 21:32:19 2010 UTC vs.
Revision 1.30 by jsr166, Mon Jun 6 19:00:28 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1995, 2010, Oracle and/or its affiliates. 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
6   * under the terms of the GNU General Public License version 2 only, as
7 < * published by the Free Software Foundation.  Sun designates this
7 > * published by the Free Software Foundation.  Oracle designates this
8   * particular file as subject to the "Classpath" exception as provided
9 < * by Sun in the LICENSE file that accompanied this code.
9 > * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# Line 77 | Line 77 | class Random implements java.io.Serializ
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;
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() { this(++seedUniquifier + System.nanoTime()); }
90 <    private static volatile long seedUniquifier = 8682522807148012L;
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 >    }
103 >
104 >    private static final AtomicLong seedUniquifier
105 >        = new AtomicLong(8682522807148012L);
106  
107      /**
108       * Creates a new random number generator using a single {@code long} seed.
# Line 103 | Line 118 | class Random implements java.io.Serializ
118       * @see   #setSeed(long)
119       */
120      public Random(long seed) {
121 <        this.seed = new AtomicLong(0L);
122 <        setSeed(seed);
121 >        if (getClass() == Random.class)
122 >            this.seed = new AtomicLong(initialScramble(seed));
123 >        else {
124 >            // subclass might have overriden setSeed
125 >            this.seed = new AtomicLong();
126 >            setSeed(seed);
127 >        }
128 >    }
129 >
130 >    private static long initialScramble(long seed) {
131 >        return (seed ^ multiplier) & mask;
132      }
133  
134      /**
# Line 127 | Line 151 | class Random implements java.io.Serializ
151       * @param seed the initial seed
152       */
153      synchronized public void setSeed(long seed) {
154 <        seed = (seed ^ multiplier) & mask;
131 <        this.seed.set(seed);
154 >        this.seed.set(initialScramble(seed));
155          haveNextNextGaussian = false;
156      }
157  
# Line 268 | Line 291 | class Random implements java.io.Serializ
291       * @return the next pseudorandom, uniformly distributed {@code int}
292       *         value between {@code 0} (inclusive) and {@code n} (exclusive)
293       *         from this random number generator's sequence
294 <     * @exception IllegalArgumentException if n is not positive
294 >     * @throws IllegalArgumentException if n is not positive
295       * @since 1.2
296       */
297  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines