/** * Returns the RandomGenerator used to generate non-secure random data. * * <p>Creates and initializes a default generator if null. * * @return the Random used to generate random data * @since 1.1 */ private RandomGenerator getRan() { if (rand == null) { rand = new JDKRandomGenerator(); rand.setSeed(System.currentTimeMillis()); } return rand; }
/** * Returns the SecureRandom used to generate secure random data. * * <p>Creates and initializes if null. Uses {@code System.currentTimeMillis() + * System.identityHashCode(this)} as the default seed. * * @return the SecureRandom used to generate secure random data, wrapped in a {@link * RandomGenerator}. */ private RandomGenerator getSecRan() { if (secRand == null) { secRand = RandomGeneratorFactory.createRandomGenerator(new SecureRandom()); secRand.setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } return secRand; }
@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); } }
/** Reseeds the random number generator with the current time in milliseconds. */ public void reSeed() { if (rand == null) { rand = new JDKRandomGenerator(); } rand.setSeed(System.currentTimeMillis()); }
/** * Reseeds the random number generator with the supplied seed. * * <p>Will create and initialize if null. * * @param seed the seed value to use */ public void reSeed(long seed) { if (rand == null) { rand = new JDKRandomGenerator(); } rand.setSeed(seed); }