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.28 by jsr166, Mon Sep 27 19:15:15 2010 UTC vs.
Revision 1.32 by dl, Wed Jan 16 19:01:22 2013 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1995, 2011, 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 26 | Line 26
26   package java.util;
27   import java.io.*;
28   import java.util.concurrent.atomic.AtomicLong;
29 + import java.util.stream.IntStream;
30 + import java.util.stream.Streams;
31 +
32   import sun.misc.Unsafe;
33  
34   /**
# Line 86 | Line 89 | class Random implements java.io.Serializ
89       * the seed of the random number generator to a value very likely
90       * to be distinct from any other invocation of this constructor.
91       */
92 <    public Random() { this(++seedUniquifier + System.nanoTime()); }
93 <    private static volatile long seedUniquifier = 8682522807148012L;
92 >    public Random() {
93 >        this(seedUniquifier() ^ System.nanoTime());
94 >    }
95 >
96 >    private static long seedUniquifier() {
97 >        // L'Ecuyer, "Tables of Linear Congruential Generators of
98 >        // Different Sizes and Good Lattice Structure", 1999
99 >        for (;;) {
100 >            long current = seedUniquifier.get();
101 >            long next = current * 181783497276652981L;
102 >            if (seedUniquifier.compareAndSet(current, next))
103 >                return next;
104 >        }
105 >    }
106 >
107 >    private static final AtomicLong seedUniquifier
108 >        = new AtomicLong(8682522807148012L);
109  
110      /**
111       * Creates a new random number generator using a single {@code long} seed.
# Line 103 | Line 121 | class Random implements java.io.Serializ
121       * @see   #setSeed(long)
122       */
123      public Random(long seed) {
124 <        this.seed = new AtomicLong(0L);
125 <        setSeed(seed);
124 >        if (getClass() == Random.class)
125 >            this.seed = new AtomicLong(initialScramble(seed));
126 >        else {
127 >            // subclass might have overriden setSeed
128 >            this.seed = new AtomicLong();
129 >            setSeed(seed);
130 >        }
131 >    }
132 >
133 >    private static long initialScramble(long seed) {
134 >        return (seed ^ multiplier) & mask;
135      }
136  
137      /**
# Line 127 | Line 154 | class Random implements java.io.Serializ
154       * @param seed the initial seed
155       */
156      synchronized public void setSeed(long seed) {
157 <        seed = (seed ^ multiplier) & mask;
131 <        this.seed.set(seed);
157 >        this.seed.set(initialScramble(seed));
158          haveNextNextGaussian = false;
159      }
160  
# Line 489 | Line 515 | class Random implements java.io.Serializ
515          }
516      }
517  
518 +    public IntStream ints() {
519 +        return Streams.generateInt(this::nextInt);
520 +    }
521 +
522      /**
523       * Serializable fields for Random.
524       *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines