25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
< |
import java.security.SecureRandom; |
29 |
< |
import java.net.InetAddress; |
28 |
> |
import java.net.NetworkInterface; |
29 |
|
import java.util.concurrent.atomic.AtomicLong; |
30 |
|
import java.util.function.IntConsumer; |
31 |
|
import java.util.function.LongConsumer; |
87 |
|
* @author Doug Lea |
88 |
|
* @since 1.8 |
89 |
|
*/ |
90 |
< |
public class SplittableRandom { |
90 |
> |
public final class SplittableRandom { |
91 |
|
|
92 |
|
/* |
93 |
|
* Implementation Overview. |
107 |
|
* Methods nextLong, nextInt, and derivatives do not return the |
108 |
|
* sequence (seed) values, but instead a hash-like bit-mix of |
109 |
|
* their bits, producing more independently distributed sequences. |
110 |
< |
* For nextLong, the mix64 bit-mixing function computes the same |
111 |
< |
* value as the "64-bit finalizer" function in Austin Appleby's |
112 |
< |
* MurmurHash3 algorithm. See |
113 |
< |
* http://code.google.com/p/smhasher/wiki/MurmurHash3 , which |
114 |
< |
* comments: "The constants for the finalizers were generated by a |
115 |
< |
* simple simulated-annealing algorithm, and both avalanche all |
116 |
< |
* bits of 'h' to within 0.25% bias." The mix32 function is |
118 |
< |
* equivalent to (int)(mix64(seed) >>> 32), but faster because it |
119 |
< |
* omits a step that doesn't contribute to result. |
110 |
> |
* For nextLong, the mix64 function is based on David Stafford's |
111 |
> |
* (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html) |
112 |
> |
* "Mix13" variant of the "64-bit finalizer" function in Austin |
113 |
> |
* Appleby's MurmurHash3 algorithm (see |
114 |
> |
* http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32 |
115 |
> |
* function is based on Stafford's Mix04 mix function, but returns |
116 |
> |
* the upper 32 bits cast as int. |
117 |
|
* |
118 |
|
* The split operation uses the current generator to form the seed |
119 |
|
* and gamma for another SplittableRandom. To conservatively |
120 |
|
* avoid potential correlations between seed and value generation, |
121 |
< |
* gamma selection (method nextGamma) uses the "Mix13" constants |
122 |
< |
* for MurmurHash3 described by David Stafford |
123 |
< |
* (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html) |
124 |
< |
* To avoid potential weaknesses in bit-mixing transformations, we |
125 |
< |
* restrict gammas to odd values with at least 12 and no more than |
126 |
< |
* 52 bits set. Rather than rejecting candidates with too few or |
127 |
< |
* too many bits set, method nextGamma flips some bits (which has |
128 |
< |
* the effect of mapping at most 4 to any given gamma value). |
132 |
< |
* This reduces the effective set of 64bit odd gamma values by |
133 |
< |
* about 2<sup>14</sup>, a very tiny percentage, and serves as an |
121 |
> |
* gamma selection (method mixGamma) uses different |
122 |
> |
* (Murmurhash3's) mix constants. To avoid potential weaknesses |
123 |
> |
* in bit-mixing transformations, we restrict gammas to odd values |
124 |
> |
* with at least 24 0-1 or 1-0 bit transitions. Rather than |
125 |
> |
* rejecting candidates with too few or too many bits set, method |
126 |
> |
* mixGamma flips some bits (which has the effect of mapping at |
127 |
> |
* most 4 to any given gamma value). This reduces the effective |
128 |
> |
* set of 64bit odd gamma values by about 2%, and serves as an |
129 |
|
* automated screening for sequence constant selection that is |
130 |
|
* left as an empirical decision in some other hashing and crypto |
131 |
|
* algorithms. |
136 |
|
* avalanching. |
137 |
|
* |
138 |
|
* The default (no-argument) constructor, in essence, invokes |
139 |
< |
* split() for a common "seeder" SplittableRandom. Unlike other |
140 |
< |
* cases, this split must be performed in a thread-safe manner, so |
141 |
< |
* we use an AtomicLong to represent the seed rather than use an |
142 |
< |
* explicit SplittableRandom. To bootstrap the seeder, we start |
143 |
< |
* off using a seed based on current time and host unless the |
144 |
< |
* SecureRandomSeed property is set. This serves as a |
145 |
< |
* slimmed-down (and insecure) variant of SecureRandom that also |
146 |
< |
* avoids stalls that may occur when using /dev/random. |
139 |
> |
* split() for a common "defaultGen" SplittableRandom. Unlike |
140 |
> |
* other cases, this split must be performed in a thread-safe |
141 |
> |
* manner, so we use an AtomicLong to represent the seed rather |
142 |
> |
* than use an explicit SplittableRandom. To bootstrap the |
143 |
> |
* defaultGen, we start off using a seed based on current time and |
144 |
> |
* network interface address unless the java.util.secureRandomSeed |
145 |
> |
* property is set. This serves as a slimmed-down (and insecure) |
146 |
> |
* variant of SecureRandom that also avoids stalls that may occur |
147 |
> |
* when using /dev/random. |
148 |
|
* |
149 |
|
* It is a relatively simple matter to apply the basic design here |
150 |
|
* to use 128 bit seeds. However, emulating 128bit arithmetic and |
157 |
|
*/ |
158 |
|
|
159 |
|
/** |
160 |
< |
* The initial gamma value for (unsplit) SplittableRandoms. Must |
161 |
< |
* be odd with at least 12 and no more than 52 bits set. Currently |
166 |
< |
* set to the golden ratio scaled to 64bits. |
160 |
> |
* The golden ratio scaled to 64bits, used as the initial gamma |
161 |
> |
* value for (unsplit) SplittableRandoms. |
162 |
|
*/ |
163 |
< |
private static final long INITIAL_GAMMA = 0x9e3779b97f4a7c15L; |
163 |
> |
private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L; |
164 |
|
|
165 |
|
/** |
166 |
|
* The least non-zero value returned by nextDouble(). This value |
167 |
|
* is scaled by a random value of 53 bits to produce a result. |
168 |
|
*/ |
169 |
< |
private static final double DOUBLE_UNIT = 1.0 / (1L << 53); |
169 |
> |
private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53); |
170 |
|
|
171 |
|
/** |
172 |
|
* The seed. Updated only via method nextSeed. |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* Computes MurmurHash3 64bit mix function. |
190 |
> |
* Computes Stafford variant 13 of 64bit mix function. |
191 |
|
*/ |
192 |
|
private static long mix64(long z) { |
193 |
< |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
194 |
< |
z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L; |
195 |
< |
return z ^ (z >>> 33); |
193 |
> |
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; |
194 |
> |
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL; |
195 |
> |
return z ^ (z >>> 31); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
< |
* Returns the 32 high bits of mix64(z) as int. |
199 |
> |
* Returns the 32 high bits of Stafford variant 4 mix64 function as int. |
200 |
|
*/ |
201 |
|
private static int mix32(long z) { |
202 |
< |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
203 |
< |
return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32); |
202 |
> |
z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L; |
203 |
> |
return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Returns the gamma value to use for a new split instance. |
208 |
|
*/ |
209 |
< |
private static long nextGamma(long z) { |
210 |
< |
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; // Stafford "Mix13" |
211 |
< |
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL; |
212 |
< |
z = (z ^ (z >>> 31)) | 1L; // force to be odd |
213 |
< |
int n = Long.bitCount(z); // ensure enough 0 and 1 bits |
214 |
< |
return (n < 12 || n > 52) ? z ^ 0xaaaaaaaaaaaaaaaaL : z; |
209 |
> |
private static long mixGamma(long z) { |
210 |
> |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants |
211 |
> |
z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L; |
212 |
> |
z = (z ^ (z >>> 33)) | 1L; // force to be odd |
213 |
> |
int n = Long.bitCount(z ^ (z >>> 1)); // ensure enough transitions |
214 |
> |
return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z; |
215 |
|
} |
216 |
|
|
217 |
|
/** |
224 |
|
/** |
225 |
|
* The seed generator for default constructors. |
226 |
|
*/ |
227 |
< |
private static final AtomicLong seeder = new AtomicLong(initialSeed()); |
227 |
> |
private static final AtomicLong defaultGen = new AtomicLong(initialSeed()); |
228 |
|
|
229 |
|
private static long initialSeed() { |
230 |
< |
try { // ignore exceptions in accessing/parsing properties |
231 |
< |
String pp = System.getProperty |
232 |
< |
("java.util.secureRandomSeed"); |
233 |
< |
if (pp != null && pp.equalsIgnoreCase("true")) { |
234 |
< |
byte[] seedBytes = java.security.SecureRandom.getSeed(8); |
235 |
< |
long s = (long)(seedBytes[0]) & 0xffL; |
236 |
< |
for (int i = 1; i < 8; ++i) |
237 |
< |
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); |
238 |
< |
return s; |
244 |
< |
} |
245 |
< |
} catch (Exception ignore) { |
230 |
> |
String pp = java.security.AccessController.doPrivileged( |
231 |
> |
new sun.security.action.GetPropertyAction( |
232 |
> |
"java.util.secureRandomSeed")); |
233 |
> |
if (pp != null && pp.equalsIgnoreCase("true")) { |
234 |
> |
byte[] seedBytes = java.security.SecureRandom.getSeed(8); |
235 |
> |
long s = (long)(seedBytes[0]) & 0xffL; |
236 |
> |
for (int i = 1; i < 8; ++i) |
237 |
> |
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); |
238 |
> |
return s; |
239 |
|
} |
240 |
< |
int hh = 0; // hashed host address |
240 |
> |
long h = 0L; |
241 |
|
try { |
242 |
< |
hh = InetAddress.getLocalHost().hashCode(); |
242 |
> |
Enumeration<NetworkInterface> ifcs = |
243 |
> |
NetworkInterface.getNetworkInterfaces(); |
244 |
> |
boolean retry = false; // retry once if getHardwareAddress is null |
245 |
> |
while (ifcs.hasMoreElements()) { |
246 |
> |
NetworkInterface ifc = ifcs.nextElement(); |
247 |
> |
if (!ifc.isVirtual()) { // skip fake addresses |
248 |
> |
byte[] bs = ifc.getHardwareAddress(); |
249 |
> |
if (bs != null) { |
250 |
> |
int n = bs.length; |
251 |
> |
int m = Math.min(n >>> 1, 4); |
252 |
> |
for (int i = 0; i < m; ++i) |
253 |
> |
h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i]; |
254 |
> |
if (m < 4) |
255 |
> |
h = (h << 8) ^ bs[n-1-m]; |
256 |
> |
h = mix64(h); |
257 |
> |
break; |
258 |
> |
} |
259 |
> |
else if (!retry) |
260 |
> |
retry = true; |
261 |
> |
else |
262 |
> |
break; |
263 |
> |
} |
264 |
> |
} |
265 |
|
} catch (Exception ignore) { |
266 |
|
} |
267 |
< |
return (mix64((((long)hh) << 32) ^ System.currentTimeMillis()) ^ |
267 |
> |
return (h ^ mix64(System.currentTimeMillis()) ^ |
268 |
|
mix64(System.nanoTime())); |
269 |
|
} |
270 |
|
|
392 |
|
* @param seed the initial seed |
393 |
|
*/ |
394 |
|
public SplittableRandom(long seed) { |
395 |
< |
this(seed, INITIAL_GAMMA); |
395 |
> |
this(seed, GOLDEN_GAMMA); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
401 |
|
* of those of any other instances in the current program; and |
402 |
|
* may, and typically does, vary across program invocations. |
403 |
|
*/ |
404 |
< |
public SplittableRandom() { // emulate seeder.split() |
405 |
< |
this.gamma = nextGamma(this.seed = seeder.addAndGet(INITIAL_GAMMA)); |
404 |
> |
public SplittableRandom() { // emulate defaultGen.split() |
405 |
> |
long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA); |
406 |
> |
this.seed = mix64(s); |
407 |
> |
this.gamma = mixGamma(s + GOLDEN_GAMMA); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
422 |
|
* @return the new SplittableRandom instance |
423 |
|
*/ |
424 |
|
public SplittableRandom split() { |
425 |
< |
long s = nextSeed(); |
409 |
< |
return new SplittableRandom(s, nextGamma(s)); |
425 |
> |
return new SplittableRandom(nextLong(), mixGamma(nextSeed())); |
426 |
|
} |
427 |
|
|
428 |
|
/** |