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 |
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 |
|
/** |
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; |
131 |
< |
this.seed.set(seed); |
157 |
> |
this.seed.set(initialScramble(seed)); |
158 |
|
haveNextNextGaussian = false; |
159 |
|
} |
160 |
|
|
515 |
|
} |
516 |
|
} |
517 |
|
|
518 |
+ |
public IntStream ints() { |
519 |
+ |
return Streams.generateInt(this::nextInt); |
520 |
+ |
} |
521 |
+ |
|
522 |
|
/** |
523 |
|
* Serializable fields for Random. |
524 |
|
* |