diff --git a/CHANGELOG.md b/CHANGELOG.md index 53deed49..e831d33c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added the SequenceSampler interface's methods to SequenceReservoirSampler. * Added the SequenceSampler interface's methods to SequencePoolSampler. * Added the SequenceSampler interface's methods to SequenceInsertionSampler. +* Methods to sample by specifying probability of including element in sample to the classes that implement SequenceSampler + including SequenceReservoirSampler, SequencePoolSampler, SequenceInsertionSampler, and SequenceCompositeSampler. ### Changed * SequenceSampler converted from a utility class of static methods to an interface, retaining the existing static diff --git a/src/main/java/org/cicirello/sequences/SequenceCompositeSampler.java b/src/main/java/org/cicirello/sequences/SequenceCompositeSampler.java index 5d24e40e..e812e537 100644 --- a/src/main/java/org/cicirello/sequences/SequenceCompositeSampler.java +++ b/src/main/java/org/cicirello/sequences/SequenceCompositeSampler.java @@ -22,6 +22,7 @@ package org.cicirello.sequences; import java.util.random.RandomGenerator; +import org.cicirello.math.rand.RandomVariates; /** * SequenceCompositeSampler generates random samples of array elements, without replacement. @@ -153,6 +154,133 @@ public T[] nextSample(T[] source, int k, T[] target) { return sample(source, k, target, r); } + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static int[] sample(int[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static long[] sample(long[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static short[] sample(short[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static byte[] sample(byte[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static double[] sample(double[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static float[] sample(float[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(char[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source String with a specified + * probability of an element's inclusion in the sample. + * + * @param source The String from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length() * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(String source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @param The type of array elements. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static T[] sample(T[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + /** * Generates a random sample of k elements, without replacement, from a given source array. All n * choose k combinations are equally likely, where n is the length of the source array. diff --git a/src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java b/src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java index 5323d296..dc167531 100644 --- a/src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java +++ b/src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java @@ -23,6 +23,7 @@ import java.util.random.RandomGenerator; import org.cicirello.math.rand.RandomSampler; +import org.cicirello.math.rand.RandomVariates; import org.cicirello.util.ArrayMinimumLengthEnforcer; /** @@ -157,6 +158,133 @@ public T[] nextSample(T[] source, int k, T[] target) { return sample(source, k, target, r); } + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static int[] sample(int[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static long[] sample(long[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static short[] sample(short[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static byte[] sample(byte[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static double[] sample(double[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static float[] sample(float[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(char[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source String with a specified + * probability of an element's inclusion in the sample. + * + * @param source The String from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length() * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(String source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @param The type of array elements. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static T[] sample(T[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + /** * Generates a random sample of k elements, without replacement, from a given source array. All n * choose k combinations are equally likely, where n is the length of the source array. diff --git a/src/main/java/org/cicirello/sequences/SequencePoolSampler.java b/src/main/java/org/cicirello/sequences/SequencePoolSampler.java index 4f975738..02dff08a 100644 --- a/src/main/java/org/cicirello/sequences/SequencePoolSampler.java +++ b/src/main/java/org/cicirello/sequences/SequencePoolSampler.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.random.RandomGenerator; import org.cicirello.math.rand.RandomIndexer; +import org.cicirello.math.rand.RandomVariates; import org.cicirello.util.ArrayMinimumLengthEnforcer; /** @@ -152,6 +153,133 @@ public T[] nextSample(T[] source, int k, T[] target) { return sample(source, k, target, r); } + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static int[] sample(int[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static long[] sample(long[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static short[] sample(short[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static byte[] sample(byte[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static double[] sample(double[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static float[] sample(float[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(char[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source String with a specified + * probability of an element's inclusion in the sample. + * + * @param source The String from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length() * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(String source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @param The type of array elements. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static T[] sample(T[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + /** * Generates a random sample of k elements, without replacement, from a given source array. All n * choose k combinations are equally likely, where n is the length of the source array. diff --git a/src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java b/src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java index fb18679e..c41ebe81 100644 --- a/src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java +++ b/src/main/java/org/cicirello/sequences/SequenceReservoirSampler.java @@ -23,6 +23,7 @@ import java.util.random.RandomGenerator; import org.cicirello.math.rand.RandomIndexer; +import org.cicirello.math.rand.RandomVariates; import org.cicirello.util.ArrayMinimumLengthEnforcer; /** @@ -150,6 +151,133 @@ public T[] nextSample(T[] source, int k, T[] target) { return sample(source, k, target, r); } + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static int[] sample(int[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static long[] sample(long[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static short[] sample(short[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static byte[] sample(byte[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static double[] sample(double[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static float[] sample(float[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(char[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source String with a specified + * probability of an element's inclusion in the sample. + * + * @param source The String from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length() * p. + * @param r The source of randomness. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static char[] sample(String source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r); + } + + /** + * Generates a random sample, without replacement, from a given source array with a specified + * probability of an element's inclusion in the sample. + * + * @param source The array from which to sample. + * @param p The probability that element is included in the sample. The expected sample size is + * source.length * p. + * @param r The source of randomness. + * @param The type of array elements. + * @return An array containing the sample, whose sample size is simply the length of the array. + */ + public static T[] sample(T[] source, double p, RandomGenerator r) { + return sample(source, RandomVariates.nextBinomial(source.length, p), null, r); + } + /** * Generates a random sample of k elements, without replacement, from a given source array. All n * choose k combinations are equally likely, where n is the length of the source array. diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerByteTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerByteTests.java index 939a4451..3730b370 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerByteTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerByteTests.java @@ -87,6 +87,30 @@ public void testSampleByteP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { byte[] allDiff = new byte[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerCharTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerCharTests.java index 81b525b4..e4fc20d5 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerCharTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerCharTests.java @@ -87,6 +87,30 @@ public void testSampleCharP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { char[] allDiff = new char[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerDoubleTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerDoubleTests.java index 168830c1..0ce48e67 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerDoubleTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerDoubleTests.java @@ -87,6 +87,30 @@ public void testSampleDoubleP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { double[] allDiff = new double[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerFloatTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerFloatTests.java index d95e6889..b9ec792b 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerFloatTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerFloatTests.java @@ -87,6 +87,30 @@ public void testSampleFloatP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { float[] allDiff = new float[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerIntTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerIntTests.java index 1cbed859..af636d2d 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerIntTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerIntTests.java @@ -87,6 +87,30 @@ public void testSampleIntP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { int[] allDiff = new int[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerLongTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerLongTests.java index 309fd627..87bae708 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerLongTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerLongTests.java @@ -87,6 +87,30 @@ public void testSampleLongP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { long[] allDiff = new long[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerObjectTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerObjectTests.java index 614ace3b..2424862e 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerObjectTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerObjectTests.java @@ -87,6 +87,58 @@ public void testSampleObjectsP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + class ObjectPSampler implements PSampler { + private final SplittableRandom r = new SplittableRandom(42); + + @Override + public T[] sample(T[] source, double p) { + return SequenceCompositeSampler.sample(source, p, r); + } + } + validateWithP(new ObjectPSampler()); + } + + @Test + public void testSamplePoolStaticP() { + class ObjectPSampler implements PSampler { + private final SplittableRandom r = new SplittableRandom(42); + + @Override + public T[] sample(T[] source, double p) { + return SequencePoolSampler.sample(source, p, r); + } + } + validateWithP(new ObjectPSampler()); + } + + @Test + public void testSampleInsertionStaticP() { + class ObjectPSampler implements PSampler { + private final SplittableRandom r = new SplittableRandom(42); + + @Override + public T[] sample(T[] source, double p) { + return SequenceInsertionSampler.sample(source, p, r); + } + } + validateWithP(new ObjectPSampler()); + } + + @Test + public void testSampleReservoirStaticP() { + class ObjectPSampler implements PSampler { + private final SplittableRandom r = new SplittableRandom(42); + + @Override + public T[] sample(T[] source, double p) { + return SequenceReservoirSampler.sample(source, p, r); + } + } + validateWithP(new ObjectPSampler()); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { Integer[] allDiff = new Integer[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerShortTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerShortTests.java index 30f87dbf..0c289376 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerShortTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerShortTests.java @@ -87,6 +87,30 @@ public void testSampleShortP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { short[] allDiff = new short[n]; diff --git a/src/test/java/org/cicirello/sequences/SequenceSamplerStringTests.java b/src/test/java/org/cicirello/sequences/SequenceSamplerStringTests.java index 51d35576..5ba1417d 100644 --- a/src/test/java/org/cicirello/sequences/SequenceSamplerStringTests.java +++ b/src/test/java/org/cicirello/sequences/SequenceSamplerStringTests.java @@ -87,6 +87,30 @@ public void testSampleStringP() { validateWithP(SequenceSampler::sample); } + @Test + public void testSampleCompositeStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceCompositeSampler.sample(source, p, r)); + } + + @Test + public void testSamplePoolStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequencePoolSampler.sample(source, p, r)); + } + + @Test + public void testSampleInsertionStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceInsertionSampler.sample(source, p, r)); + } + + @Test + public void testSampleReservoirStaticP() { + SplittableRandom r = new SplittableRandom(42); + validateWithP((source, p) -> SequenceReservoirSampler.sample(source, p, r)); + } + private void validateWithP(PSampler sampler) { for (int n = 1; n <= 10; n++) { char[] allDiff = new char[n];