--- jsr166/src/main/java/util/SplittableRandom.java 2015/09/19 21:07:10 1.28 +++ jsr166/src/main/java/util/SplittableRandom.java 2016/11/12 03:15:13 1.34 @@ -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 @@ -529,6 +531,20 @@ public final class SplittableRandom { } /** + * Generates a pseudorandom number with the indicated number of + * bits. Unlike in superclass @{link Random}, this method is never + * internally called or used by any other publicly accessible + * method. + * + * @param bits random bits + * @return the next pseudorandom value from this random number + * generator's sequence + */ + protected int next(int bits) { + return (int)(nextLong() >>> (64 - bits)); + } + + /** * Returns a pseudorandom {@code double} value between the specified * origin (inclusive) and bound (exclusive). * @@ -825,7 +841,8 @@ public final class SplittableRandom { * approach. The long and double versions of this class are * identical except for types. */ - static final class RandomIntsSpliterator implements Spliterator.OfInt { + private static final class RandomIntsSpliterator + implements Spliterator.OfInt { final SplittableRandom rng; long index; final long fence; @@ -880,7 +897,8 @@ public final class SplittableRandom { /** * Spliterator for long streams. */ - static final class RandomLongsSpliterator implements Spliterator.OfLong { + private static final class RandomLongsSpliterator + implements Spliterator.OfLong { final SplittableRandom rng; long index; final long fence; @@ -936,7 +954,8 @@ public final class SplittableRandom { /** * Spliterator for double streams. */ - static final class RandomDoublesSpliterator implements Spliterator.OfDouble { + private static final class RandomDoublesSpliterator + implements Spliterator.OfDouble { final SplittableRandom rng; long index; final long fence;