RNG-59: Ensure seed diversity.
Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/350fb428 Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/350fb428 Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/350fb428 Branch: refs/heads/master Commit: 350fb428eaf42a0ece8ebc59714201d590f6ff94 Parents: f4a4aad Author: Gilles <er...@apache.org> Authored: Thu Oct 25 17:20:34 2018 +0200 Committer: Gilles <er...@apache.org> Committed: Thu Oct 25 17:20:34 2018 +0200 ---------------------------------------------------------------------- .../apache/commons/rng/simple/internal/SeedFactory.java | 10 ++++++---- src/changes/changes.xml | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rng/blob/350fb428/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java index 0b7f51f..617c6d3 100644 --- a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java +++ b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/SeedFactory.java @@ -16,6 +16,7 @@ */ package org.apache.commons.rng.simple.internal; +import java.security.SecureRandom; import org.apache.commons.rng.core.util.NumberFactory; import org.apache.commons.rng.core.source32.RandomIntSource; import org.apache.commons.rng.core.source32.Well44497b; @@ -46,10 +47,11 @@ public final class SeedFactory { private static final RandomIntSource SEED_GENERATOR; static { - // Another RNG for initializing the "SEED_GENERATOR". - final long t = System.currentTimeMillis(); - final int h = System.identityHashCode(Runtime.getRuntime()); - final SplitMix64 rng = new SplitMix64(t ^ NumberFactory.makeLong(h, ~h)); + // Use a secure RNG so that different instances (e.g. in multiple JVM + // instances started in rapid succession) will have different seeds. + final SecureRandom seedGen = new SecureRandom(); + final long initSeed = NumberFactory.makeLong(seedGen.generateSeed(8)); + final SplitMix64 rng = new SplitMix64(initSeed); final int blockCount = 1391; // Size of the state array of "Well44497b". SEED_GENERATOR = new Well44497b(createIntArray(blockCount, rng)); http://git-wip-us.apache.org/repos/asf/commons-rng/blob/350fb428/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0e002e3..62557fe 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -67,6 +67,9 @@ Additional code is provided in the following module: It is however not part of the official API and no compatibility should be expected in subsequent releases. "> + <action dev="erans" type="fix" issue="RNG-59"> + Use JDK's "SecureRandom" to seed the "SeedFactory". + </action> <action dev="erans" type="update" issue="RNG-58"> Allow part of RNG state to be contained in base classes, e.g. to enable caching in common code (see RNG-57).