@Test public void testNextUniformIAE() { try { randomData.nextUniform(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(0, Double.POSITIVE_INFINITY); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(Double.NEGATIVE_INFINITY, 0); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(0, Double.NaN); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(Double.NaN, 0); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } }
/** test exclusive endpoints of nextUniform * */ @Test public void testNextUniformExclusiveEndpoints() { for (int i = 0; i < 1000; i++) { double u = randomData.nextUniform(0.99, 1); Assert.assertTrue(u > 0.99 && u < 1); } }
private void checkNextUniformUniform(double min, double max) { // Set up bin bounds - min, binBound[0], ..., binBound[binCount-2], max final int binCount = 5; final double binSize = max / binCount - min / binCount; // Prevent overflow in extreme value case final double[] binBounds = new double[binCount - 1]; binBounds[0] = min + binSize; for (int i = 1; i < binCount - 1; i++) { binBounds[i] = binBounds[i - 1] + binSize; // + instead of * to avoid overflow in extreme case } final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final double value = randomData.nextUniform(min, max); Assert.assertTrue("nextUniform range", (value > min) && (value < max)); // Find bin int j = 0; while (j < binCount - 1 && value > binBounds[j]) { j++; } freq.addValue(j); } final long[] observed = new long[binCount]; for (int i = 0; i < binCount; i++) { observed[i] = freq.getCount(i); } final double[] expected = new double[binCount]; for (int i = 0; i < binCount; i++) { expected[i] = 1d / binCount; } TestUtils.assertChiSquareAccept(expected, observed, 0.01); }
@Test public void testNextPoissonConsistency() { // Small integral means for (int i = 1; i < 100; i++) { checkNextPoissonConsistency(i); } // non-integer means for (int i = 1; i < 10; i++) { checkNextPoissonConsistency(randomData.nextUniform(1, 1000)); } // large means for (int i = 1; i < 10; i++) { checkNextPoissonConsistency(randomData.nextUniform(1000, 10000)); } }
/** 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 testNextUniformExtremeValues() { double x = randomData.nextUniform(-Double.MAX_VALUE, Double.MAX_VALUE); double y = randomData.nextUniform(-Double.MAX_VALUE, Double.MAX_VALUE); Assert.assertFalse(x == y); Assert.assertFalse(Double.isNaN(x)); Assert.assertFalse(Double.isNaN(y)); Assert.assertFalse(Double.isInfinite(x)); Assert.assertFalse(Double.isInfinite(y)); }
@Setup public void generateData() { src = new double[arraySize]; final RandomDataGenerator gen = new RandomDataGenerator(); for (int i = 0; i < src.length; i++) { if (i < src.length / 3) { src[i] = gen.nextUniform(0, 1); } else { src[i] = gen.nextGamma(42., 42.); } } }
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 testNextInversionDeviate() { // Set the seed for the default random generator RandomGenerator rg = new Well19937c(100); RandomDataGenerator rdg = new RandomDataGenerator(rg); double[] quantiles = new double[10]; for (int i = 0; i < 10; i++) { quantiles[i] = rdg.nextUniform(0, 1); } // Reseed again so the inversion generator gets the same sequence rg.setSeed(100); BetaDistribution betaDistribution = new BetaDistribution(rg, 2, 4, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); /* * Generate a sequence of deviates using inversion - the distribution function * evaluated at the random value from the distribution should match the uniform * random value used to generate it, which is stored in the quantiles[] array. */ for (int i = 0; i < 10; i++) { double value = betaDistribution.sample(); Assert.assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9); } }
@Override public CADensity createRandomIndividual(RandomDataGenerator random) { double density = random.nextUniform(lower, upper); return new CADensity(density, testLength); }