Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit a254d20e0e5ec936f6bf7ebee4a86322094db247 1 parent a1c5ccc
@dicej dicej authored
Showing with 10 additions and 2 deletions.
  1. +10 −2 classpath/java/util/Random.java
View
12 classpath/java/util/Random.java
@@ -13,7 +13,9 @@
public class Random {
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;
@@ -22,7 +24,13 @@ public Random(long seed) {
}
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) {
Please sign in to comment.
Something went wrong with that request. Please try again.