1 |
|
/* |
2 |
< |
* Copyright 1995-2007 Sun Microsystems, Inc. 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 |
18 |
|
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
* |
21 |
< |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
< |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
< |
* have any questions. |
21 |
> |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
> |
* or visit www.oracle.com if you need additional information or have any |
23 |
> |
* questions. |
24 |
|
*/ |
25 |
|
|
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 |
|
/** |
35 |
|
* An instance of this class is used to generate a stream of |
36 |
|
* pseudorandom numbers. The class uses a 48-bit seed, which is |
37 |
|
* modified using a linear congruential formula. (See Donald Knuth, |
38 |
< |
* <i>The Art of Computer Programming, Volume 3</i>, Section 3.2.1.) |
38 |
> |
* <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.) |
39 |
|
* <p> |
40 |
|
* If two instances of {@code Random} are created with the same |
41 |
|
* seed, and the same sequence of method calls is made for each, they |
53 |
|
* <p> |
54 |
|
* Many applications will find the method {@link Math#random} simpler to use. |
55 |
|
* |
56 |
+ |
* <p>Instances of {@code java.util.Random} are threadsafe. |
57 |
+ |
* However, the concurrent use of the same {@code java.util.Random} |
58 |
+ |
* instance across threads may encounter contention and consequent |
59 |
+ |
* poor performance. Consider instead using |
60 |
+ |
* {@link java.util.concurrent.ThreadLocalRandom} in multithreaded |
61 |
+ |
* designs. |
62 |
+ |
* |
63 |
+ |
* <p>Instances of {@code java.util.Random} are not cryptographically |
64 |
+ |
* secure. Consider instead using {@link java.security.SecureRandom} to |
65 |
+ |
* get a cryptographically secure pseudo-random number generator for use |
66 |
+ |
* by security-sensitive applications. |
67 |
+ |
* |
68 |
|
* @author Frank Yellin |
69 |
|
* @since 1.0 |
70 |
|
*/ |
80 |
|
*/ |
81 |
|
private final AtomicLong seed; |
82 |
|
|
83 |
< |
private final static long multiplier = 0x5DEECE66DL; |
84 |
< |
private final static long addend = 0xBL; |
85 |
< |
private final static long mask = (1L << 48) - 1; |
83 |
> |
private static final long multiplier = 0x5DEECE66DL; |
84 |
> |
private static final long addend = 0xBL; |
85 |
> |
private static final long mask = (1L << 48) - 1; |
86 |
|
|
87 |
|
/** |
88 |
|
* Creates a new random number generator. This constructor sets |
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. |
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 |
|
/** |
154 |
|
* @param seed the initial seed |
155 |
|
*/ |
156 |
|
synchronized public void setSeed(long seed) { |
157 |
< |
seed = (seed ^ multiplier) & mask; |
119 |
< |
this.seed.set(seed); |
157 |
> |
this.seed.set(initialScramble(seed)); |
158 |
|
haveNextNextGaussian = false; |
159 |
|
} |
160 |
|
|
294 |
|
* @return the next pseudorandom, uniformly distributed {@code int} |
295 |
|
* value between {@code 0} (inclusive) and {@code n} (exclusive) |
296 |
|
* from this random number generator's sequence |
297 |
< |
* @exception IllegalArgumentException if n is not positive |
297 |
> |
* @throws IllegalArgumentException if n is not positive |
298 |
|
* @since 1.2 |
299 |
|
*/ |
300 |
|
|
515 |
|
} |
516 |
|
} |
517 |
|
|
518 |
+ |
public IntStream ints() { |
519 |
+ |
return Streams.generateInt(this::nextInt); |
520 |
+ |
} |
521 |
+ |
|
522 |
|
/** |
523 |
|
* Serializable fields for Random. |
524 |
|
* |