--- jsr166/src/main/java/util/SplittableRandom.java 2015/09/20 02:41:36 1.29 +++ jsr166/src/main/java/util/SplittableRandom.java 2018/12/02 23:06:16 1.41 @@ -80,7 +80,7 @@ import java.util.stream.StreamSupport; * in security-sensitive applications. Additionally, * default-constructed instances do not use a cryptographically random * seed unless the {@linkplain System#getProperty system property} - * {@code java.util.secureRandomSeed} is set to {@code true}. + * {@systemProperty java.util.secureRandomSeed} is set to {@code true}. * * @author Guy Steele * @author Doug Lea @@ -219,31 +219,33 @@ public final class SplittableRandom { return seed += gamma; } + // IllegalArgumentException messages + static final String BAD_BOUND = "bound must be positive"; + static final String BAD_RANGE = "bound must be greater than origin"; + static final String BAD_SIZE = "size must be non-negative"; + /** * The seed generator for default constructors. */ - private static final AtomicLong defaultGen = new AtomicLong(initialSeed()); - - private static long initialSeed() { - String pp = java.security.AccessController.doPrivileged( - new sun.security.action.GetPropertyAction( - "java.util.secureRandomSeed")); - if (pp != null && pp.equalsIgnoreCase("true")) { + private static final AtomicLong defaultGen + = new AtomicLong(mix64(System.currentTimeMillis()) ^ + mix64(System.nanoTime())); + + // at end of to survive static initialization circularity + static { + if (java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public Boolean run() { + return Boolean.getBoolean("java.util.secureRandomSeed"); + }})) { byte[] seedBytes = java.security.SecureRandom.getSeed(8); - long s = (long)(seedBytes[0]) & 0xffL; + long s = (long)seedBytes[0] & 0xffL; for (int i = 1; i < 8; ++i) - s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); - return s; + s = (s << 8) | ((long)seedBytes[i] & 0xffL); + defaultGen.set(s); } - return (mix64(System.currentTimeMillis()) ^ - mix64(System.nanoTime())); } - // IllegalArgumentException messages - static final String BAD_BOUND = "bound must be positive"; - static final String BAD_RANGE = "bound must be greater than origin"; - static final String BAD_SIZE = "size must be non-negative"; - /* * Internal versions of nextX methods used by streams, as well as * the public nextX(origin, bound) methods. These exist mainly to @@ -373,7 +375,7 @@ public final class SplittableRandom { * may, and typically does, vary across program invocations. */ public SplittableRandom() { // emulate defaultGen.split() - long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA); + long s = defaultGen.getAndAdd(GOLDEN_GAMMA << 1); this.seed = mix64(s); this.gamma = mixGamma(s + GOLDEN_GAMMA); } @@ -397,6 +399,26 @@ public final class SplittableRandom { } /** + * Fills a user-supplied byte array with generated pseudorandom bytes. + * + * @param bytes the byte array to fill with pseudorandom bytes + * @throws NullPointerException if bytes is null + * @since 10 + */ + public void nextBytes(byte[] bytes) { + int i = 0; + int len = bytes.length; + for (int words = len >> 3; words--> 0; ) { + long rnd = nextLong(); + for (int n = 8; n--> 0; rnd >>>= Byte.SIZE) + bytes[i++] = (byte)rnd; + } + if (i < len) + for (long rnd = nextLong(); i < len; rnd >>>= Byte.SIZE) + bytes[i++] = (byte)rnd; + } + + /** * Returns a pseudorandom {@code int} value. * * @return a pseudorandom {@code int} value @@ -777,8 +799,7 @@ public final class SplittableRandom { * @return a stream of pseudorandom {@code double} values, * each with the given origin (inclusive) and bound (exclusive) * @throws IllegalArgumentException if {@code streamSize} is - * less than zero - * @throws IllegalArgumentException if {@code randomNumberOrigin} + * less than zero, or {@code randomNumberOrigin} * is greater than or equal to {@code randomNumberBound} */ public DoubleStream doubles(long streamSize, double randomNumberOrigin,