This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 2b24bf79a3bb119cb0389771971b32e2a5a796e4 Author: aherbert <aherb...@apache.org> AuthorDate: Thu Feb 14 12:26:03 2019 +0000 Formatted code and javadoc --- .../distribution/GeometricSamplersPerformance.java | 5 ++-- .../sampling/distribution/GeometricSampler.java | 14 +++++---- .../distribution/GeometricSamplerTest.java | 33 ++++++++++++---------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java index 60e86ad..bf3cdf9 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/GeometricSamplersPerformance.java @@ -134,8 +134,9 @@ public class GeometricSamplersPerformance { // --- // Note: if cumulativeProbability == 0 then log1p(-0) is zero and the result // after the range check is 0. - // Note: if cumulativeProbability == 1 then log1p(-1) is negative infinity, the result of - // the divide is positive infinity and the result after the range check is Integer.MAX_VALUE. + // Note: if cumulativeProbability == 1 then log1p(-1) is negative infinity, the result + // of the divide is positive infinity and the result after the range check is + // Integer.MAX_VALUE. return Math.max(0, (int) Math.ceil(Math.log1p(-cumulativeProbability) / log1mProbabilityOfSuccess - 1)); } } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GeometricSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GeometricSampler.java index ba62ac4..2197d4c 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GeometricSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GeometricSampler.java @@ -78,7 +78,8 @@ public class GeometricSampler implements DiscreteSampler { /** * @param rng Generator of uniformly distributed random numbers - * @param probabilityOfSuccess The probability of success (must be {@code 0<p<1}) + * @param probabilityOfSuccess The probability of success (must be in the range + * {@code [0 < probabilityOfSuccess < 1]}) */ GeometricExponentialSampler(UniformRandomProvider rng, double probabilityOfSuccess) { this.rng = rng; @@ -116,13 +117,14 @@ public class GeometricSampler implements DiscreteSampler { * * @param rng Generator of uniformly distributed random numbers * @param probabilityOfSuccess The probability of success - * @throws IllegalArgumentException if {@code probabilityOfSuccess} is not in the range [0 < - * probabilityOfSuccess <= 1] + * @throws IllegalArgumentException if {@code probabilityOfSuccess} is not in the range + * {@code [0 < probabilityOfSuccess <= 1]}) */ public GeometricSampler(UniformRandomProvider rng, double probabilityOfSuccess) { if (probabilityOfSuccess <= 0 || probabilityOfSuccess > 1) { throw new IllegalArgumentException( - "Probability of success must be in the range [0 < p <= 1]: " + probabilityOfSuccess); + "Probability of success (p) must be in the range [0 < p <= 1]: " + + probabilityOfSuccess); } delegate = probabilityOfSuccess == 1 ? GeometricP1Sampler.INSTANCE : @@ -132,8 +134,8 @@ public class GeometricSampler implements DiscreteSampler { /** * Create a sample from a geometric distribution. * - * <p>The sample will take the values in the set {@code [0, 1, 2, ...]}, equivalent to the number of - * failures before the first success. + * <p>The sample will take the values in the set {@code [0, 1, 2, ...]}, equivalent to the + * number of failures before the first success. */ @Override public int sample() { diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java index 00f1000..c3267bf 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GeometricSamplerTest.java @@ -17,6 +17,7 @@ package org.apache.commons.rng.sampling.distribution; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.core.source64.SplitMix64; import org.apache.commons.rng.simple.RandomSource; import org.junit.Assert; import org.junit.Test; @@ -26,8 +27,8 @@ import org.junit.Test; */ public class GeometricSamplerTest { /** - * Test the edge case where the probability of success is 1. This is a valid geometric distribution - * where the sample should always be 0. + * Test the edge case where the probability of success is 1. This is a valid geometric + * distribution where the sample should always be 0. */ @Test public void testProbabilityOfSuccessIsOneGeneratesZeroForSamples() { @@ -45,18 +46,20 @@ public class GeometricSamplerTest { */ @Test public void testProbabilityOfSuccessIsOneSamplerToString() { - final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64); - final GeometricSampler sampler = new GeometricSampler(rng, 1); - Assert.assertTrue("Missing 'Geometric' from toString", sampler.toString().contains("Geometric")); + final UniformRandomProvider unusedRng = new SplitMix64(0L); + final GeometricSampler sampler = new GeometricSampler(unusedRng, 1); + Assert.assertTrue("Missing 'Geometric' from toString", + sampler.toString().contains("Geometric")); } /** * Test the edge case where the probability of success is nearly 0. This is a valid geometric - * distribution but the sample is clipped to max integer value because the underlying exponential - * has a mean of positive infinity (effectively the sample is from a truncated distribution). + * distribution but the sample is clipped to max integer value because the underlying + * exponential has a mean of positive infinity (effectively the sample is from a truncated + * distribution). * - * <p>This test can be changed in future if a lower bound limit for the probability of success is - * introduced. + * <p>This test can be changed in future if a lower bound limit for the probability of success + * is introduced. */ @Test public void testProbabilityOfSuccessIsAlmostZeroGeneratesMaxValueForSamples() { @@ -64,8 +67,8 @@ public class GeometricSamplerTest { final GeometricSampler sampler = new GeometricSampler(rng, Double.MIN_VALUE); // All samples should be max value for (int i = 0; i < 10; i++) { - Assert.assertEquals("p=(almost 0) should have Integer.MAX_VALUE for all samples", Integer.MAX_VALUE, - sampler.sample()); + Assert.assertEquals("p=(almost 0) should have Integer.MAX_VALUE for all samples", + Integer.MAX_VALUE, sampler.sample()); } } @@ -75,8 +78,8 @@ public class GeometricSamplerTest { @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testProbabilityOfSuccessAboveOneThrows() { - final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, Long.valueOf(0)); - new GeometricSampler(rng, Math.nextUp(1.0)); + final UniformRandomProvider unusedRng = new SplitMix64(0L); + new GeometricSampler(unusedRng, Math.nextUp(1.0)); } /** @@ -85,7 +88,7 @@ public class GeometricSamplerTest { @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testProbabilityOfSuccessIsZeroThrows() { - final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, Long.valueOf(0)); - new GeometricSampler(rng, 0); + final UniformRandomProvider unusedRng = new SplitMix64(0L); + new GeometricSampler(unusedRng, 0); } }