Java Code Examples for org.apache.commons.rng.simple.RandomSource#createLong()

The following examples show how to use org.apache.commons.rng.simple.RandomSource#createLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PoissonSamplerCacheTest.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Check poisson samples are the same from the {@link PoissonSampler}
 * and {@link PoissonSamplerCache}.
 *
 * @param minMean the min mean of the cache
 * @param maxMean the max mean of the cache
 */
private void checkComputeSameSamplesAsPoissonSampler(int minMean,
                                                     int maxMean) {
    // Two identical RNGs
    final RandomSource source = RandomSource.SPLIT_MIX_64;
    final long seed = RandomSource.createLong();
    final RestorableUniformRandomProvider rng1 =
            RandomSource.create(source, seed);
    final RestorableUniformRandomProvider rng2 =
            RandomSource.create(source, seed);

    // Create the cache with the given range
    final PoissonSamplerCache cache =
            createPoissonSamplerCache(minMean, maxMean);
    // Test all means in the test range (which may be different
    // from the cache range).
    for (int i = minRange; i <= maxRange; i++) {
        // Test integer mean (no SmallMeanPoissonSampler required)
        testPoissonSamples(rng1, rng2, cache, i);
        // Test non-integer mean (SmallMeanPoissonSampler required)
        testPoissonSamples(rng1, rng2, cache, i + 0.5);
    }
}
 
Example 2
Source File: DiscreteUniformSamplerTest.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
private static void assertOffsetSamples(int range) {
    final Long seed = RandomSource.createLong();
    final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);
    final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);
    final UniformRandomProvider rng3 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);

    // Since the upper limit is inclusive
    range = range - 1;
    final int offsetLo = -13;
    final int offsetHi = 42;
    final SharedStateDiscreteSampler sampler = DiscreteUniformSampler.of(rng1, 0, range);
    final SharedStateDiscreteSampler samplerLo = DiscreteUniformSampler.of(rng2, offsetLo, offsetLo + range);
    final SharedStateDiscreteSampler samplerHi = DiscreteUniformSampler.of(rng3, offsetHi, offsetHi + range);
    for (int i = 0; i < 10; i++) {
        final int sample1 = sampler.sample();
        final int sample2 = samplerLo.sample();
        final int sample3 = samplerHi.sample();
        Assert.assertEquals("Incorrect negative offset sample", sample1 + offsetLo, sample2);
        Assert.assertEquals("Incorrect positive offset sample", sample1 + offsetHi, sample3);
    }
}
 
Example 3
Source File: ListSamplerTest.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Assert the shuffle matches {@link PermutationSampler#shuffle(UniformRandomProvider, int[])}.
 *
 * @param list Array whose entries will be shuffled (in-place).
 */
private static void assertShuffleMatchesPermutationSamplerShuffle(List<Integer> list) {
    final int[] array = new int[list.size()];
    ListIterator<Integer> it = list.listIterator();
    for (int i = 0; i < array.length; i++) {
        array[i] = it.next();
    }

    // Identical RNGs
    final long seed = RandomSource.createLong();
    final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);
    final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);

    ListSampler.shuffle(rng1, list);
    PermutationSampler.shuffle(rng2, array);

    final String msg = "Type=" + list.getClass().getSimpleName();
    it = list.listIterator();
    for (int i = 0; i < array.length; i++) {
        Assert.assertEquals(msg, array[i], it.next().intValue());
    }
}
 
Example 4
Source File: ListSamplerTest.java    From commons-rng with Apache License 2.0 6 votes vote down vote up
/**
 * Assert the shuffle matches {@link PermutationSampler#shuffle(UniformRandomProvider, int[], int, boolean)}.
 *
 * @param list Array whose entries will be shuffled (in-place).
 * @param start Index at which shuffling begins.
 * @param towardHead Shuffling is performed for index positions between
 * {@code start} and either the end (if {@code false}) or the beginning
 * (if {@code true}) of the array.
 */
private static void assertShuffleMatchesPermutationSamplerShuffle(List<Integer> list,
                                                                int start,
                                                                boolean towardHead) {
    final int[] array = new int[list.size()];
    ListIterator<Integer> it = list.listIterator();
    for (int i = 0; i < array.length; i++) {
        array[i] = it.next();
    }

    // Identical RNGs
    final long seed = RandomSource.createLong();
    final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);
    final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, seed);

    ListSampler.shuffle(rng1, list, start, towardHead);
    PermutationSampler.shuffle(rng2, array, start, towardHead);

    final String msg = String.format("Type=%s start=%d towardHead=%b",
            list.getClass().getSimpleName(), start, towardHead);
    it = list.listIterator();
    for (int i = 0; i < array.length; i++) {
        Assert.assertEquals(msg, array[i], it.next().intValue());
    }
}
 
Example 5
Source File: PoissonSamplerCacheTest.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Check poisson samples are the same from the {@link PoissonSampler}
 * and a {@link PoissonSamplerCache} created reusing values.
 *
 * <p>Note: This cannot check the cache values were reused but ensures
 * that a new cache created with a range functions correctly.
 *
 * @param minMean  the min mean of the cache
 * @param maxMean  the max mean of the cache
 * @param minMean2 the min mean of the second cache
 * @param maxMean2 the max mean of the second cache
 */
private void checkComputeSameSamplesAsPoissonSamplerReusingCache(int minMean,
                                                                 int maxMean,
                                                                 int minMean2,
                                                                 int maxMean2) {
    // Two identical RNGs
    final RandomSource source = RandomSource.SPLIT_MIX_64;
    final long seed = RandomSource.createLong();
    final RestorableUniformRandomProvider rng1 =
            RandomSource.create(source, seed);
    final RestorableUniformRandomProvider rng2 =
            RandomSource.create(source, seed);

    // Create the cache with the given range and fill it
    final PoissonSamplerCache cache =
            createPoissonSamplerCache(minMean, maxMean);
    // Test all means in the test range (which may be different
    // from the cache range).
    for (int i = minMean; i <= maxMean; i++) {
        cache.createSharedStateSampler(rng1, i);
    }

    final PoissonSamplerCache cache2 = cache.withRange(minMean2, maxMean2);
    Assert.assertNotSame("WithRange cache is the same object", cache, cache2);

    // Test all means in the test range (which may be different
    // from the cache range).
    for (int i = minRange; i <= maxRange; i++) {
        // Test integer mean (no SmallMeanPoissonSampler required)
        testPoissonSamples(rng1, rng2, cache2, i);
        // Test non-integer mean (SmallMeanPoissonSampler required)
        testPoissonSamples(rng1, rng2, cache2, i + 0.5);
    }
}
 
Example 6
Source File: RngDataOutputTest.java    From commons-rng with Apache License 2.0 4 votes vote down vote up
/**
 * Assert the byte output from the source is the same. Creates two instances of the same
 * RNG with the same seed. The first is converted to a new RNG using the {@code rngConverter}.
 * This is used to output raw bytes using a {@link DataOutputStream} via the specified
 * {@code pipe}. The second is used to output raw bytes via the {@link RngDataOutput} class
 * created using the {@code factory}.
 *
 * <p>The random source should output native {@code int} or {@code long} values.
 *
 * @param source Random source.
 * @param rngConverter Converter for the raw RNG.
 * @param pipe Pipe to send data from the RNG to the DataOutputStream.
 * @param factory Factory for the RngDataOutput.
 * @param byteOrder Byte order for the RngDataOutput.
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void assertRngOutput(RandomSource source,
    UnaryOperator<UniformRandomProvider> rngConverter,
    BiConsumer<DataOutputStream, UniformRandomProvider> pipe,
    RngDataOutputFactory factory,
    ByteOrder byteOrder) throws IOException {
    final long seed = RandomSource.createLong();
    UniformRandomProvider rng1 = RandomSource.create(source, seed);
    UniformRandomProvider rng2 = RandomSource.create(source, seed);
    final int size = 37;
    for (int repeats = 1; repeats <= 2; repeats++) {
        byte[] expected = createBytes(rng1, size, repeats, rngConverter, pipe);
        byte[] actual = writeRngOutput(rng2, size, repeats, byteOrder, factory);
        Assert.assertArrayEquals(expected, actual);
    }
}