@Test public void testNextGamma() { double[] quartiles; long[] counts; // Tests shape > 1, one case in the rejection sampling quartiles = TestUtils.getDistributionQuartiles(new GammaDistribution(4, 2)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGamma(4, 2); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); // Tests shape <= 1, another case in the rejection sampling quartiles = TestUtils.getDistributionQuartiles(new GammaDistribution(0.3, 3)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGamma(0.3, 3); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); }
@Test public void testNextZipf() { ZipfDistributionTest testInstance = new ZipfDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); ZipfDistribution distribution = (ZipfDistribution) testInstance.makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } randomData.reSeed(1000); for (int i = 0; i < sampleSize; i++) { int value = randomData.nextZipf(distribution.getNumberOfElements(), distribution.getExponent()); for (int j = 0; j < length; j++) { if (value == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); }
/** 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); }
public static void main(String[] args) { // int d_total = 10000; int p_numbers = 3; // Partition Numbers int p_size = 5; int z_exponent = 1; int d = 0; int d_start = 0; int d_end = p_size; double mu = 0.0; double sigma = 1.0; double z, c, c_rand, n; ZipfDistribution zipf; // Parameters = Number of Elements, Exponent RandomDataGenerator randData = new RandomDataGenerator(); RandomDataGenerator rand = new RandomDataGenerator(); rand.reSeed(0); for (int p = 0; p < p_numbers; p++) { zipf = new ZipfDistribution(p_size, z_exponent); zipf.reseedRandomGenerator(p); randData.reSeed(p); for (d = d_start; d < d_end; d++) { z = randData.nextZipf(p_size, z_exponent); z += d_start; if (z > d_end) z -= 1; c = zipf.cumulativeProbability((d - d_start)); c_rand = zipf.cumulativeProbability(((int) z - d_start)); // n = rand.nextGaussian(mu, sigma); n = rand.nextUniform(0.0, 1.0); System.out.println(p + " " + d + " " + c + " " + (int) z + " " + c_rand + " " + n); } d_start = d_end; d_end = (d + p_size); } }
@Test public void testNextWeibull() { double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextWeibull(1.2, 2.1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); }
/** test failure modes and distribution of nextExponential() */ @Test public void testNextExponential() { try { randomData.nextExponential(-1); Assert.fail("negative mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextExponential(0); Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } double[] quartiles; long[] counts; // Mean 1 quartiles = TestUtils.getDistributionQuartiles(new ExponentialDistribution(1)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextExponential(1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); // Mean 5 quartiles = TestUtils.getDistributionQuartiles(new ExponentialDistribution(5)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextExponential(5); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); }
/** test failure modes and distribution of nextGaussian() */ @Test public void testNextGaussian() { try { randomData.nextGaussian(0, 0); Assert.fail("zero sigma -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } double[] quartiles = TestUtils.getDistributionQuartiles(new NormalDistribution(0, 1)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGaussian(0, 1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); }
/** * Make sure that empirical distribution of random Poisson(4)'s has P(X <= 5) close to actual * cumulative Poisson probability and that nextPoisson fails when mean is non-positive. */ @Test public void testNextPoisson() { try { randomData.nextPoisson(0); Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextPoisson(-1); Assert.fail("negative mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextPoisson(0); Assert.fail("0 mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } final double mean = 4.0d; final int len = 5; PoissonDistribution poissonDistribution = new PoissonDistribution(mean); Frequency f = new Frequency(); randomData.reSeed(1000); for (int i = 0; i < largeSampleSize; i++) { f.addValue(randomData.nextPoisson(mean)); } final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = f.getCount(i + 1); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = poissonDistribution.probability(i + 1) * largeSampleSize; } TestUtils.assertChiSquareAccept(expected, observed, 0.0001); }