/** test reseeding, algorithm/provider games */ @Test public void testConfig() { randomData.reSeed(1000); double v = randomData.nextUniform(0, 1); randomData.reSeed(); Assert.assertTrue("different seeds", Math.abs(v - randomData.nextUniform(0, 1)) > 10E-12); randomData.reSeed(1000); Assert.assertEquals("same seeds", v, randomData.nextUniform(0, 1), 10E-12); randomData.reSeedSecure(1000); String hex = randomData.nextSecureHexString(40); randomData.reSeedSecure(); Assert.assertTrue("different seeds", !hex.equals(randomData.nextSecureHexString(40))); randomData.reSeedSecure(1000); Assert.assertTrue("same seeds", !hex.equals(randomData.nextSecureHexString(40))); /* * remove this test back soon, since it takes about 4 seconds * * try { randomData.setSecureAlgorithm("SHA1PRNG","SUN"); } catch * (NoSuchProviderException ex) { ; } Assert.assertTrue("different seeds", * !hex.equals(randomData.nextSecureHexString(40))); try { * randomData.setSecureAlgorithm("NOSUCHTHING","SUN"); * Assert.fail("expecting NoSuchAlgorithmException"); } catch * (NoSuchProviderException ex) { ; } catch (NoSuchAlgorithmException * ex) { ; } * * try { randomData.setSecureAlgorithm("SHA1PRNG","NOSUCHPROVIDER"); * Assert.fail("expecting NoSuchProviderException"); } catch * (NoSuchProviderException ex) { ; } */ // test reseeding without first using the generators RandomDataGenerator rd = new RandomDataGenerator(); rd.reSeed(100); rd.nextLong(1, 2); RandomDataGenerator rd2 = new RandomDataGenerator(); rd2.reSeedSecure(2000); rd2.nextSecureLong(1, 2); rd = new RandomDataGenerator(); rd.reSeed(); rd.nextLong(1, 2); rd2 = new RandomDataGenerator(); rd2.reSeedSecure(); rd2.nextSecureLong(1, 2); }
@Test public void testNextLongIAE() { try { randomData.nextLong(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } }
@Test public void testNextLongWideRange() { long lower = -0x6543210FEDCBA987L; long upper = 0x456789ABCDEF0123L; long max = Long.MIN_VALUE; long min = Long.MAX_VALUE; for (int i = 0; i < 10000000; ++i) { long r = randomData.nextLong(lower, upper); max = FastMath.max(max, r); min = FastMath.min(min, r); Assert.assertTrue(r >= lower); Assert.assertTrue(r <= upper); } double ratio = (((double) max) - ((double) min)) / (((double) upper) - ((double) lower)); Assert.assertTrue(ratio > 0.99999); }
private void checkNextLongUniform(long min, long max) { final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final long value = randomData.nextLong(min, max); Assert.assertTrue( "nextLong range: " + value + " " + min + " " + max, (value >= min) && (value <= max)); freq.addValue(value); } final int len = ((int) (max - min)) + 1; final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = freq.getCount(min + i); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = 1d / len; } TestUtils.assertChiSquareAccept(expected, observed, 0.01); }
@Test public void testNextLongExtremeValues() { long x = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); long y = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); Assert.assertFalse(x == y); }