mirror of
https://github.com/corda/corda.git
synced 2025-01-06 05:04:20 +00:00
improve seed generation in java.util.Random
The previous code caused frequent seed collisions for successive calls to the no-arg constructor, even for single threaded workloads. This patch should avoid such collisions in both single and multi-threaded cases.
This commit is contained in:
parent
a1c5cccd96
commit
a254d20e0e
@ -13,7 +13,9 @@ package java.util;
|
|||||||
public class Random {
|
public class Random {
|
||||||
private static final long Mask = 0x5DEECE66DL;
|
private static final long Mask = 0x5DEECE66DL;
|
||||||
|
|
||||||
private static long nextSeed = 0;
|
private static final long InitialSeed = 123456789987654321L;
|
||||||
|
|
||||||
|
private static long nextSeed = InitialSeed;
|
||||||
|
|
||||||
private long seed;
|
private long seed;
|
||||||
|
|
||||||
@ -22,7 +24,13 @@ public class Random {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Random() {
|
public Random() {
|
||||||
setSeed((nextSeed++) ^ System.currentTimeMillis());
|
synchronized (Random.class) {
|
||||||
|
setSeed(nextSeed ^ System.currentTimeMillis());
|
||||||
|
nextSeed *= 123456789987654321L;
|
||||||
|
if (nextSeed == 0) {
|
||||||
|
nextSeed = InitialSeed;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSeed(long seed) {
|
public void setSeed(long seed) {
|
||||||
|
Loading…
Reference in New Issue
Block a user