# HG changeset patch # User bpb # Date 1430346555 25200 # Wed Apr 29 15:29:15 2015 -0700 # Node ID d03d820bfd269619fad78a9dc68aff602e8574be # Parent 83ff0dedf9e1660fefff1edc4b85debd3af7377e 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used. Reviewed-by: XXX diff --git a/test/java/lang/Double/ParseHexFloatingPoint.java b/test/java/lang/Double/ParseHexFloatingPoint.java --- a/test/java/lang/Double/ParseHexFloatingPoint.java +++ b/test/java/lang/Double/ParseHexFloatingPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,15 +23,15 @@ /* * @test - * @bug 4826774 - * @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main ParseHexFloatingPoint + * @bug 4826774 8078672 + * @summary Numerical tests for hexadecimal inputs to parse{Double, Float} (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy * @key randomness */ - -import java.util.regex.*; - public class ParseHexFloatingPoint { private ParseHexFloatingPoint(){} @@ -256,7 +256,7 @@ failures += significandAlignmentTests(); { - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); // Consistency check; double => hexadecimal => double // preserves the original value. for(int i = 0; i < 1000; i++) { diff --git a/test/java/lang/Integer/BitTwiddle.java b/test/java/lang/Integer/BitTwiddle.java --- a/test/java/lang/Integer/BitTwiddle.java +++ b/test/java/lang/Integer/BitTwiddle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4495754 - * @summary Basic test for int bit twiddling + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main BitTwiddle + * @bug 4495754 8078672 + * @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed) * @author Josh Bloch * @key randomness */ @@ -36,7 +39,7 @@ private static final int N = 1000; // # of repetitions per test public static void main(String args[]) { - Random rnd = new Random(); + Random rnd = RandomFactory.getRandom(); if (highestOneBit(0) != 0) throw new RuntimeException("a"); diff --git a/test/java/lang/Long/BitTwiddle.java b/test/java/lang/Long/BitTwiddle.java --- a/test/java/lang/Long/BitTwiddle.java +++ b/test/java/lang/Long/BitTwiddle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4495754 - * @summary Basic test for long bit twiddling + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main BitTwiddle + * @bug 4495754 8078672 + * @summary Basic test for long bit twiddling (use -Dseed=X to set PRNG seed) * @author Josh Bloch * @key randomness */ @@ -36,7 +39,7 @@ private static final int N = 1000; // # of repetitions per test public static void main(String args[]) { - Random rnd = new Random(); + Random rnd = RandomFactory.getRandom(); if (highestOneBit(0) != 0) throw new RuntimeException("a"); diff --git a/test/java/lang/Math/CubeRootTests.java b/test/java/lang/Math/CubeRootTests.java --- a/test/java/lang/Math/CubeRootTests.java +++ b/test/java/lang/Math/CubeRootTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4347132 4939441 - * @summary Tests for {Math, StrictMath}.cbrt + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main CubeRootTests + * @bug 4347132 4939441 8078672 + * @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy * @key randomness */ @@ -36,7 +39,7 @@ static final double NaNd = Double.NaN; // Initialize shared random number generator - static java.util.Random rand = new java.util.Random(); + static java.util.Random rand = RandomFactory.getRandom(); static int testCubeRootCase(double input, double expected) { int failures=0; diff --git a/test/java/lang/Math/HypotTests.java b/test/java/lang/Math/HypotTests.java --- a/test/java/lang/Math/HypotTests.java +++ b/test/java/lang/Math/HypotTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4851638 4939441 - * @summary Tests for {Math, StrictMath}.hypot + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main HypotTests + * @bug 4851638 4939441 8078672 + * @summary Tests for {Math, StrictMath}.hypot (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy * @key randomness */ @@ -120,7 +123,7 @@ * exponent). While the exponent of r is less than or equal * to (MAX_EXPONENT - 3), the computation should not overflow. */ - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); for(int i = 0; i < 1000; i++) { double d = rand.nextDouble(); // Scale d to have an exponent equal to MAX_EXPONENT -15 diff --git a/test/java/lang/Math/IeeeRecommendedTests.java b/test/java/lang/Math/IeeeRecommendedTests.java --- a/test/java/lang/Math/IeeeRecommendedTests.java +++ b/test/java/lang/Math/IeeeRecommendedTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4860891 4826732 4780454 4939441 4826652 - * @summary Tests for IEEE 754[R] recommended functions and similar methods + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main IeeeRecommendedTests + * @bug 4860891 4826732 4780454 4939441 4826652 8078672 + * @summary Tests for IEEE 754[R] recommended functions and similar methods (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy * @key randomness */ @@ -46,7 +49,7 @@ static final double Double_MAX_SUBNORMALmm = 0x0.ffffffffffffeP-1022; // Initialize shared random number generator - static java.util.Random rand = new java.util.Random(); + static java.util.Random rand = RandomFactory.getRandom(); /** * Returns a floating-point power of two in the normal range. diff --git a/test/java/lang/Math/Log1pTests.java b/test/java/lang/Math/Log1pTests.java --- a/test/java/lang/Math/Log1pTests.java +++ b/test/java/lang/Math/Log1pTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,11 @@ /* * @test - * @bug 4851638 4939441 - * @summary Tests for {Math, StrictMath}.log1p + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main Log1pTests + * @bug 4851638 4939441 8078672 + * @summary Tests for {Math, StrictMath}.log1p (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy * @key randomness */ @@ -99,7 +102,7 @@ // Construct random values with exponents ranging from -53 to // 52 and compare against HP-15C formula. - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); for(int i = 0; i < 1000; i++) { double d = rand.nextDouble(); diff --git a/test/java/math/BigDecimal/StringConstructor.java b/test/java/math/BigDecimal/StringConstructor.java --- a/test/java/math/BigDecimal/StringConstructor.java +++ b/test/java/math/BigDecimal/StringConstructor.java @@ -23,21 +23,20 @@ /* * @test - * @library .. - * @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460 + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main StringConstructor + * @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460 8078672 * @summary Tests the BigDecimal string constructor (use -Dseed=X to set PRNG seed). * @key randomness */ import java.math.*; +import java.util.Random; public class StringConstructor { - private static RandomSeed rndSeed = new RandomSeed(false); - public static void main(String[] args) throws Exception { - System.out.println("Random number generator seed = " + rndSeed.getSeed()); - constructWithError(""); constructWithError("+"); constructWithError("-"); @@ -72,13 +71,14 @@ nonAsciiZeroTest(); // Roundtrip tests + Random random = RandomFactory.getRandom(); for (int i=0; i<100; i++) { - int size = rndSeed.getRandom().nextInt(100) + 1; - BigInteger bi = new BigInteger(size, rndSeed.getRandom()); - if (rndSeed.getRandom().nextBoolean()) + int size = random.nextInt(100) + 1; + BigInteger bi = new BigInteger(size, random); + if (random.nextBoolean()) bi = bi.negate(); int decimalLength = bi.toString().length(); - int scale = rndSeed.getRandom().nextInt(decimalLength); + int scale = random.nextInt(decimalLength); BigDecimal bd = new BigDecimal(bi, scale); String bdString = bd.toString(); // System.err.println("bi" + bi.toString() + "\tscale " + scale); diff --git a/test/java/math/BigInteger/BigIntegerTest.java b/test/java/math/BigInteger/BigIntegerTest.java --- a/test/java/math/BigInteger/BigIntegerTest.java +++ b/test/java/math/BigInteger/BigIntegerTest.java @@ -23,8 +23,10 @@ /* * @test - * @library .. - * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 4026465 8074460 + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main BigIntegerTest + * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 4026465 8074460 8078672 * @summary tests methods in BigInteger (use -Dseed=X to set PRNG seed) * @run main/timeout=400 BigIntegerTest * @author madbot @@ -37,6 +39,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; +import java.util.Random; /** * This is a simple test class created to ensure that the results @@ -87,7 +90,7 @@ static final int SIZE = 1000; // numbers per batch - private static RandomSeed rndSeed = new RandomSeed(false); + private static Random random = RandomFactory.getRandom(); static boolean failure = false; @@ -99,7 +102,7 @@ int arrayLength = 23; int halfLength = arrayLength/2; byte[] array = new byte[arrayLength]; - rndSeed.getRandom().nextBytes(array); + random.nextBytes(array); int[][] offLen = new int[][] { // offset, length, num exceptions {-1, arrayLength, 1}, // negative offset @@ -163,7 +166,7 @@ } byte[] magNonZeroLength = new byte[42]; - rndSeed.getRandom().nextBytes(magNonZeroLength); + random.nextBytes(magNonZeroLength); for (int signum = -1; signum <= 1; signum++) { BigInteger bi = new BigInteger(signum, magNonZeroLength, 0, 0); if (bi.compareTo(BigInteger.ZERO) != 0) { @@ -176,13 +179,13 @@ for (int i = 0; i < SIZE; i++) { // create reference value via a different code path from those tested - BigInteger reference = new BigInteger(2 + rndSeed.getRandom().nextInt(336), 4, rndSeed.getRandom()); + BigInteger reference = new BigInteger(2 + random.nextInt(336), 4, random); byte[] refArray = reference.toByteArray(); int refLen = refArray.length; - int factor = rndSeed.getRandom().nextInt(5); - int objLen = refArray.length + factor*rndSeed.getRandom().nextInt(refArray.length) + 1; - int offset = rndSeed.getRandom().nextInt(objLen - refLen); + int factor = random.nextInt(5); + int objLen = refArray.length + factor*random.nextInt(refArray.length) + 1; + int offset = random.nextInt(objLen - refLen); byte[] objArray = new byte[objLen]; System.arraycopy(refArray, 0, objArray, offset, refLen); @@ -193,7 +196,7 @@ failCount++; } - boolean isNegative = rndSeed.getRandom().nextBoolean(); + boolean isNegative = random.nextBoolean(); BigInteger signMag = new BigInteger(isNegative ? -1 : 1, objArray, offset, refLen); if (signMag.compareTo(isNegative ? reference.negate() : reference) != 0) { System.err.println("Sign-magnitude BigInteger not equal for offset " + @@ -210,7 +213,7 @@ for (int i=0; i= lower; bits--) { for (int i = 0; i < 50; i++) { - BigInteger x = BigInteger.ONE.shiftLeft(bits - 1).or(new BigInteger(bits - 2, rndSeed.getRandom())); + BigInteger x = BigInteger.ONE.shiftLeft(bits - 1).or(new BigInteger(bits - 2, random)); for (int radix = Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) { String result = x.toString(radix); @@ -766,9 +769,9 @@ int failCount = 0; for (int i=0; i<10; i++) { - BigInteger m = new BigInteger(100, 5, rndSeed.getRandom()); + BigInteger m = new BigInteger(100, 5, random); while(m.compareTo(BigInteger.ONE) != 1) - m = new BigInteger(100, 5, rndSeed.getRandom()); + m = new BigInteger(100, 5, random); BigInteger exp = m.subtract(BigInteger.ONE); BigInteger base = fetchNumber(order).abs(); while(base.compareTo(m) != -1) @@ -828,7 +831,7 @@ // Test consistency for(int i=0; i<10; i++) { - p1 = BigInteger.probablePrime(100, rndSeed.getRandom()); + p1 = BigInteger.probablePrime(100, random); if (!p1.isProbablePrime(100)) { System.err.println("Consistency "+p1.toString(16)); failCount++; @@ -869,7 +872,7 @@ // Numbers of the form (6k+1)(12k+1)(18k+1) are Carmichael numbers if // each of the factors is prime int found = 0; - BigInteger f1 = new BigInteger(40, 100, rndSeed.getRandom()); + BigInteger f1 = new BigInteger(40, 100, random); while (found < NUM_CARMICHAELS_TO_TEST) { BigInteger k = null; BigInteger f2, f3; @@ -896,8 +899,8 @@ // Test some composites that are products of 2 primes for (int i=0; i<50; i++) { - p1 = BigInteger.probablePrime(100, rndSeed.getRandom()); - p2 = BigInteger.probablePrime(100, rndSeed.getRandom()); + p1 = BigInteger.probablePrime(100, random); + p2 = BigInteger.probablePrime(100, random); c1 = p1.multiply(p2); if (c1.isProbablePrime(100)) { System.err.println("Composite failed "+c1.toString(16)); @@ -906,8 +909,8 @@ } for (int i=0; i<4; i++) { - p1 = BigInteger.probablePrime(600, rndSeed.getRandom()); - p2 = BigInteger.probablePrime(600, rndSeed.getRandom()); + p1 = BigInteger.probablePrime(600, random); + p2 = BigInteger.probablePrime(600, random); c1 = p1.multiply(p2); if (c1.isProbablePrime(100)) { System.err.println("Composite failed "+c1.toString(16)); @@ -962,7 +965,7 @@ // Next, pick some large primes, use nextProbablePrime to find the // next one, and make sure there are no primes in between for (int i=0; i<100; i+=10) { - p1 = BigInteger.probablePrime(50 + i, rndSeed.getRandom()); + p1 = BigInteger.probablePrime(50 + i, random); p2 = p1.add(ONE); p3 = p1.nextProbablePrime(); while(p2.compareTo(p3) < 0) { @@ -1027,7 +1030,7 @@ } for(int i=0; i<10; i++) { - BigInteger b1 = fetchNumber(rndSeed.getRandom().nextInt(100)); + BigInteger b1 = fetchNumber(random.nextInt(100)); BigInteger b2 = null; File f = new File("serialtest"); try (FileOutputStream fos = new FileOutputStream(f)) { @@ -1061,8 +1064,6 @@ * */ public static void main(String[] args) throws Exception { - System.out.println("Random number generator seed = " + rndSeed.getSeed()); - // Some variables for sizing test numbers in bits int order1 = ORDER_MEDIUM; int order2 = ORDER_SMALL; @@ -1134,8 +1135,8 @@ * If order is less than 2, order is changed to 2. */ private static BigInteger fetchNumber(int order) { - boolean negative = rndSeed.getRandom().nextBoolean(); - int numType = rndSeed.getRandom().nextInt(7); + boolean negative = random.nextBoolean(); + int numType = random.nextInt(7); BigInteger result = null; if (order < 2) order = 2; @@ -1159,14 +1160,14 @@ break; case 3: // One bit in number - result = BigInteger.ONE.shiftLeft(rndSeed.getRandom().nextInt(order)); + result = BigInteger.ONE.shiftLeft(random.nextInt(order)); break; case 4: // Random bit density byte[] val = new byte[(order+7)/8]; - int iterations = rndSeed.getRandom().nextInt(order); + int iterations = random.nextInt(order); for (int i=0; i 0) { - int runLength = Math.min(remaining, rndSeed.getRandom().nextInt(order)); + int runLength = Math.min(remaining, random.nextInt(order)); result = result.shiftLeft(runLength); if (bit > 0) result = result.add(ONE.shiftLeft(runLength).subtract(ONE)); @@ -1186,7 +1187,7 @@ break; default: // random bits - result = new BigInteger(order, rndSeed.getRandom()); + result = new BigInteger(order, random); } if (negative) diff --git a/test/java/math/BigInteger/ModPow65537.java b/test/java/math/BigInteger/ModPow65537.java --- a/test/java/math/BigInteger/ModPow65537.java +++ b/test/java/math/BigInteger/ModPow65537.java @@ -23,8 +23,10 @@ /* * @test - * @library .. - * @bug 4891312 8074460 + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main ModPow65537 + * @bug 4891312 8074460 8078672 * @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed) * @author Andreas Sterbenz * @key randomness @@ -34,6 +36,7 @@ import java.security.*; import java.security.spec.*; +import java.util.Random; public class ModPow65537 { @@ -79,9 +82,8 @@ private static void testSigning(KeyPair kp) throws Exception { System.out.println(kp.getPublic()); byte[] data = new byte[1024]; - RandomSeed rndSeed = new RandomSeed(false); - System.out.println("Random number generator seed = " + rndSeed.getSeed()); - rndSeed.getRandom().nextBytes(data); + Random random = RandomFactory.getRandom(); + random.nextBytes(data); Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign"); sig.initSign(kp.getPrivate()); diff --git a/test/java/math/BigInteger/PrimeTest.java b/test/java/math/BigInteger/PrimeTest.java --- a/test/java/math/BigInteger/PrimeTest.java +++ b/test/java/math/BigInteger/PrimeTest.java @@ -25,8 +25,10 @@ /* * @test - * @library .. - * @bug 8026236 8074460 + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* + * @run main PrimeTest + * @bug 8026236 8074460 8078672 * @summary test primality verification methods in BigInteger (use -Dseed=X to set PRNG seed) * @author bpb * @key randomness @@ -179,9 +181,8 @@ } // Create a list of non-prime BigIntegers. - RandomSeed rndSeed = new RandomSeed(true); - System.out.println("Random number generator seed = " + rndSeed.getSeed()); - List nonPrimeBigInts = (rndSeed.getSplittableRandom()) + SplittableRandom splitRandom = RandomFactory.getSplittableRandom(); + List nonPrimeBigInts = (splitRandom) .ints(NUM_NON_PRIMES, 2, maxPrime).mapToObj(BigInteger::valueOf) .filter(b -> !b.isProbablePrime(certainty)).collect(toList()); diff --git a/test/java/math/BigInteger/SymmetricRangeTests.java b/test/java/math/BigInteger/SymmetricRangeTests.java --- a/test/java/math/BigInteger/SymmetricRangeTests.java +++ b/test/java/math/BigInteger/SymmetricRangeTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 1025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,9 +24,10 @@ /* * @test * @ignore This test has huge memory requirements - * @library .. + * @library /lib/testlibrary/ + * @build jdk.testlibrary.* * @run main/timeout=180/othervm -Xmx8g SymmetricRangeTests - * @bug 6910473 8021204 8021203 9005933 8074460 + * @bug 6910473 8021204 8021203 9005933 8074460 8078672 * @summary Test range of BigInteger values (use -Dseed=X to set PRNG seed) * @author Dmitry Nadezhin * @key randomness @@ -38,6 +39,7 @@ import java.io.ObjectOutputStream; import java.util.Arrays; import java.math.BigInteger; +import java.util.Random; public class SymmetricRangeTests { @@ -115,9 +117,8 @@ System.out.println("Testing overflow in BitSieve.sieveSingle"); int bitLength = (5 << 27) - 1; try { - RandomSeed rndSeed = new RandomSeed(false); - System.out.println("Random number generator seed = " + rndSeed.getSeed()); - BigInteger actual = new BigInteger(bitLength, 0, rndSeed.getRandom()); + Random random = RandomFactory.getRandom(); + BigInteger actual = new BigInteger(bitLength, 0, random); throw new RuntimeException("new BigInteger(bitLength, 0, null).bitLength()=" + actual.bitLength()); } catch (ArithmeticException e) { // expected diff --git a/test/java/math/RandomSeed.java b/test/java/math/RandomSeed.java deleted file mode 100644 --- a/test/java/math/RandomSeed.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.util.Random; -import java.util.SplittableRandom; - -public class RandomSeed { - private long seed = 0L; - private Random rnd = null; - private SplittableRandom srnd = null; - - public RandomSeed(boolean isSplittableRandom) { - init(isSplittableRandom); - } - - private void init(boolean isSplittableRandom) { - // obtain seed from environment if supplied - boolean isSeedProvided = false; - try { - // note that Long.valueOf(null) also throws a NumberFormatException - // so if the property is undefined this will still work correctly - seed = Long.valueOf(System.getProperty("seed")); - isSeedProvided = true; - } catch (NumberFormatException e) { - // do nothing: isSeedProvided is already false - } - - // if no seed from environment, create a fresh one - Random tmpRnd = null; - if (!isSeedProvided) { - tmpRnd = new Random(); - seed = tmpRnd.nextLong(); - } - - // create the PRNG - if (isSplittableRandom) { - srnd = new SplittableRandom(seed); - } else { - rnd = tmpRnd != null ? tmpRnd : new Random(); - rnd.setSeed(seed); - } - } - - public Random getRandom() { - if (rnd == null) { - throw new IllegalStateException("Variable of type Random not initialized"); - } - return rnd; - } - - public SplittableRandom getSplittableRandom() { - if (srnd == null) { - throw new IllegalStateException("Variable of type SplittableRandom not initialized"); - } - return srnd; - } - - public long getSeed() { - return seed; - } -} diff --git a/test/lib/testlibrary/jdk/testlibrary/RandomFactory.java b/test/lib/testlibrary/jdk/testlibrary/RandomFactory.java new file mode 100644 --- /dev/null +++ b/test/lib/testlibrary/jdk/testlibrary/RandomFactory.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.util.Random; +import java.util.SplittableRandom; + +/** + * Factory class which generates and prints to STDOUT a long-valued seed + * for use in initializing a PRNG. An instance of {@code Random} or + * {@code SplittableRandom} may likewise be obtained. + */ +public class RandomFactory { + /** + * Attempt to obtain the seed from the value of the "seed" property. + * @return The seed or {@code null} if the "seed" property was not set or + * could not be parsed. + */ + private static Long getSystemSeed() { + Long seed = null; + try { + // note that Long.valueOf(null) also throws a + // NumberFormatException so if the property is undefined this + // will still work correctly + seed = Long.valueOf(System.getProperty("seed")); + } catch (NumberFormatException e) { + // do nothing: seed is still null + } + + return seed; + } + + /** + * Obtain a seed from an independent PRNG. + * + * @return A random seed. + */ + private static long getRandomSeed() { + return new Random().nextLong(); + } + + /** + * Obtain and print to STDOUT a seed appropriate for initializing a PRNG. + * If the system property "seed" is set and has value which may be correctly + * parsed it is used, otherwise a seed is generated using an independent + * PRNG. + * + * @return The seed. + */ + public static long getSeed() { + Long seed = getSystemSeed(); + if (seed == null) { + seed = getRandomSeed(); + } + System.out.println("Seed from RandomFactory = "+seed+"L"); + return seed; + } + + /** + * Obtain and print to STDOUT a seed and use it to initialize a new + * {@code Random} instance which is returned. If the system + * property "seed" is set and has value which may be correctly parsed it + * is used, otherwise a seed is generated using an independent PRNG. + * + * @return The {@code Random} instance. + */ + public static Random getRandom() { + return new Random(getSeed()); + } + + /** + * Obtain and print to STDOUT a seed and use it to initialize a new + * {@code SplittableRandom} instance which is returned. If the system + * property "seed" is set and has value which may be correctly parsed it + * is used, otherwise a seed is generated using an independent PRNG. + * + * @return The {@code SplittableRandom} instance. + */ + public static SplittableRandom getSplittableRandom() { + return new SplittableRandom(getSeed()); + } +}