You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/03/06 15:46:57 UTC

[commons-rng] branch master updated (fccc73e -> f5ac625)

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git.


    from fccc73e  InternalUtilsTest: Test for NegativeArraySizeException
     new fbeeb08  RNG-73: Add methods used from UniformRandomProvider to the samplers.
     new cf51f2c  Merge branch 'improvement-RNG-73'
     new bfef2b6  Track changes.
     new ac929bf  RNG-79: Benchmark methods for producing nextDouble and nextFloat
     new d03726b  RNG-79: Updated testFloatGeneration internal order
     new 5a6c2f5  RNG-79: Updated testFloat/DoubleGeneration delta.
     new d4950ef  RNG-79: NumberFactoryTest floating-point precision uses commons-math3
     new 80c94a5  RNG-79: Modify benchmark to use uniform random generator.
     new 5262744  Merge branch 'feature-RNG-79'
     new f5ac625  Track changes.

The 10 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 commons-rng-core/pom.xml                           |   7 +
 .../commons/rng/core/util/NumberFactoryTest.java   | 103 +++++++++----
 .../rng/examples/jmh/NextDoublePerformance.java    | 162 +++++++++++++++++++++
 .../commons/rng/sampling/CollectionSampler.java    |   2 +
 .../commons/rng/sampling/CombinationSampler.java   |   8 +-
 .../DiscreteProbabilityCollectionSampler.java      |   2 +
 .../apache/commons/rng/sampling/ListSampler.java   |  10 +-
 .../commons/rng/sampling/PermutationSampler.java   |   8 +-
 .../commons/rng/sampling/SubsetSamplerUtils.java   |   6 +-
 .../commons/rng/sampling/UnitSphereSampler.java    |   7 +
 .../AhrensDieterExponentialSampler.java            |   2 +
 .../AhrensDieterMarsagliaTsangGammaSampler.java    |   7 +
 .../distribution/BoxMullerGaussianSampler.java     |   2 +
 .../distribution/BoxMullerLogNormalSampler.java    |   2 +
 .../BoxMullerNormalizedGaussianSampler.java        |   2 +
 .../sampling/distribution/ChengBetaSampler.java    |   2 +
 .../distribution/ContinuousUniformSampler.java     |   2 +
 .../distribution/DiscreteUniformSampler.java       |   4 +
 .../sampling/distribution/GeometricSampler.java    |  10 +-
 .../InverseTransformContinuousSampler.java         |   4 +-
 .../InverseTransformDiscreteSampler.java           |   4 +-
 .../InverseTransformParetoSampler.java             |   2 +
 .../distribution/LargeMeanPoissonSampler.java      |  11 +-
 .../MarsagliaNormalizedGaussianSampler.java        |   2 +
 .../rng/sampling/distribution/PoissonSampler.java  |   7 +
 .../sampling/distribution/PoissonSamplerCache.java |  19 ++-
 .../RejectionInversionZipfSampler.java             |   2 +
 .../distribution/SmallMeanPoissonSampler.java      |   8 +-
 .../ZigguratNormalizedGaussianSampler.java         |  11 +-
 src/changes/changes.xml                            |   6 +
 30 files changed, 371 insertions(+), 53 deletions(-)
 create mode 100644 commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java


[commons-rng] 04/10: RNG-79: Updated testFloat/DoubleGeneration delta.

Posted by ah...@apache.org.
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 5a6c2f5f0402fa27efa3b2bcdd2d131a5dc971f6
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Mar 4 22:41:19 2019 +0000

    RNG-79: Updated testFloat/DoubleGeneration delta.
    
    The delta is set to be within 1 or 2 ulp of 1.
---
 .../commons/rng/core/util/NumberFactoryTest.java     | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
index 5638768..6fb909e 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
@@ -145,10 +145,12 @@ public class NumberFactoryTest {
     public void testFloatGeneration() {
         final int allBits = 0xffffffff;
 
-        // Not capable of generating 1
-        Assert.assertEquals(1, (allBits >>> 9) * 0x1.0p-23f, 1e-6);
-        Assert.assertEquals(1, (allBits >>> 8) * 0x1.0p-24f, 1e-6);
-        Assert.assertEquals(1, Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, 1e-6);
+        // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1.
+        final float deltaUlp2 = 1f - Math.nextAfter(Math.nextAfter(1f, -1), -1);
+        final float deltaUlp1 = 1f - Math.nextAfter(1f, -1);
+        Assert.assertEquals(1, (allBits >>> 9) * 0x1.0p-23f, deltaUlp2);
+        Assert.assertEquals(1, (allBits >>> 8) * 0x1.0p-24f, deltaUlp1);
+        Assert.assertEquals(1, Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, deltaUlp2);
 
         final int noBits = 0;
         Assert.assertEquals(0, (noBits >>> 9) * 0x1.0p-23f, 0);
@@ -160,10 +162,12 @@ public class NumberFactoryTest {
     public void testDoubleGeneration() {
         final long allBits = 0xffffffffffffffffL;
 
-        // Not capable of generating 1
-        Assert.assertEquals(1, (allBits >>> 12) * 0x1.0p-52d, 1e-10);
-        Assert.assertEquals(1, (allBits >>> 11) * 0x1.0p-53d, 1e-10);
-        Assert.assertEquals(1, Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, 1e-10);
+        // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1.
+        final double deltaUlp2 = 1 - Math.nextAfter(Math.nextAfter(1, -1), -1);
+        final double deltaUlp1 = 1 - Math.nextAfter(1, -1);
+        Assert.assertEquals(1, (allBits >>> 12) * 0x1.0p-52d, deltaUlp2);
+        Assert.assertEquals(1, (allBits >>> 11) * 0x1.0p-53d, deltaUlp1);
+        Assert.assertEquals(1, Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, deltaUlp2);
 
         final long noBits = 0;
         Assert.assertEquals(0, (noBits >>> 12) * 0x1.0p-52d, 0);


[commons-rng] 01/10: RNG-73: Add methods used from UniformRandomProvider to the samplers.

Posted by ah...@apache.org.
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 fbeeb08208c1f92f7ae395451f86a32f54564fe6
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Feb 28 21:15:19 2019 +0000

    RNG-73: Add methods used from UniformRandomProvider to the samplers.
---
 .../commons/rng/sampling/CollectionSampler.java       |  2 ++
 .../commons/rng/sampling/CombinationSampler.java      |  8 +++++---
 .../DiscreteProbabilityCollectionSampler.java         |  2 ++
 .../org/apache/commons/rng/sampling/ListSampler.java  | 10 +++++++++-
 .../commons/rng/sampling/PermutationSampler.java      |  8 ++++++--
 .../commons/rng/sampling/SubsetSamplerUtils.java      |  6 ++++--
 .../commons/rng/sampling/UnitSphereSampler.java       |  7 +++++++
 .../distribution/AhrensDieterExponentialSampler.java  |  2 ++
 .../AhrensDieterMarsagliaTsangGammaSampler.java       |  7 +++++++
 .../distribution/BoxMullerGaussianSampler.java        |  2 ++
 .../distribution/BoxMullerLogNormalSampler.java       |  2 ++
 .../BoxMullerNormalizedGaussianSampler.java           |  2 ++
 .../rng/sampling/distribution/ChengBetaSampler.java   |  2 ++
 .../distribution/ContinuousUniformSampler.java        |  2 ++
 .../sampling/distribution/DiscreteUniformSampler.java |  4 ++++
 .../rng/sampling/distribution/GeometricSampler.java   | 10 ++++++----
 .../InverseTransformContinuousSampler.java            |  4 +++-
 .../distribution/InverseTransformDiscreteSampler.java |  4 +++-
 .../distribution/InverseTransformParetoSampler.java   |  2 ++
 .../distribution/LargeMeanPoissonSampler.java         | 11 +++++++++--
 .../MarsagliaNormalizedGaussianSampler.java           |  2 ++
 .../rng/sampling/distribution/PoissonSampler.java     |  7 +++++++
 .../sampling/distribution/PoissonSamplerCache.java    | 19 +++++++++++++------
 .../distribution/RejectionInversionZipfSampler.java   |  2 ++
 .../distribution/SmallMeanPoissonSampler.java         |  8 +++++---
 .../ZigguratNormalizedGaussianSampler.java            | 11 +++++++++--
 26 files changed, 119 insertions(+), 27 deletions(-)

diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CollectionSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CollectionSampler.java
index 34c7247..54f9ee9 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CollectionSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CollectionSampler.java
@@ -26,6 +26,8 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Sampling from a {@link Collection}.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
+ *
  * @param <T> Type of items in the collection.
  *
  * @since 1.0
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CombinationSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CombinationSampler.java
index 58d4fe8..eeae0d5 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CombinationSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CombinationSampler.java
@@ -26,14 +26,16 @@ import org.apache.commons.rng.UniformRandomProvider;
  * <p>A combination is a selection of items from a collection, such that (unlike
  * permutations) the order of selection <strong>does not matter</strong>. This
  * sampler can be used to generate a combination in an unspecified order and is
- * faster than the corresponding {@link PermutationSampler}.
+ * faster than the corresponding {@link PermutationSampler}.</p>
  *
  * <p>Note that the sample order is unspecified. For example a sample
  * combination of 2 from 4 may return {@code [0,1]} or {@code [1,0]} as the two are
- * equivalent, and the order of a given combination may change in subsequent samples.
+ * equivalent, and the order of a given combination may change in subsequent samples.</p>
  *
  * <p>The sampler can be used to generate indices to select subsets where the
- * order of the subset is not important.
+ * order of the subset is not important.</p>
+ *
+ * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
  *
  * @see PermutationSampler
  */
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSampler.java
index f85ff14..9bdeaab 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/DiscreteProbabilityCollectionSampler.java
@@ -32,6 +32,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * Note that if all unique items are assigned the same probability,
  * it is much more efficient to use {@link CollectionSampler}.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @param <T> Type of items in the collection.
  *
  * @since 1.1
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/ListSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/ListSampler.java
index 40e4ddd..db31105 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/ListSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/ListSampler.java
@@ -25,7 +25,7 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Sampling from a {@link List}.
  *
- * This class also contains utilities for shuffling a {@link List} in-place.
+ * <p>This class also contains utilities for shuffling a {@link List} in-place.</p>
  *
  * @since 1.0
  */
@@ -45,6 +45,10 @@ public class ListSampler {
      * contains identical objects, the sample may include repeats.
      * </p>
      *
+     * <p>
+     * Sampling uses {@link UniformRandomProvider#nextInt(int)}.
+     * </p>
+     *
      * @param <T> Type of the list items.
      * @param rng Generator of uniformly distributed random numbers.
      * @param collection List to be sampled from.
@@ -89,6 +93,10 @@ public class ListSampler {
      * The {@code start} and {@code pos} parameters select which part
      * of the array is randomized and which is left untouched.
      *
+     * <p>
+     * Sampling uses {@link UniformRandomProvider#nextInt(int)}.
+     * </p>
+     *
      * @param <T> Type of the list items.
      * @param rng Random number generator.
      * @param list List whose entries will be shuffled (in-place).
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/PermutationSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/PermutationSampler.java
index a5a30c2..7cb72cc 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/PermutationSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/PermutationSampler.java
@@ -23,7 +23,9 @@ import org.apache.commons.rng.UniformRandomProvider;
  * Class for representing <a href="https://en.wikipedia.org/wiki/Permutation">permutations</a>
  * of a sequence of integers.
  *
- * <p>This class also contains utilities for shuffling an {@code int[]} array in-place.
+ * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
+ *
+ * <p>This class also contains utilities for shuffling an {@code int[]} array in-place.</p>
  */
 public class PermutationSampler {
     /** Domain of the permutation. */
@@ -40,7 +42,7 @@ public class PermutationSampler {
      * length {@code k} whose entries are selected randomly, without
      * repetition, from the integers 0, 1, ..., {@code n}-1 (inclusive).
      * The returned array represents a permutation of {@code n} taken
-     * {@code k}.
+     * {@code k}.</p>
      *
      * @param rng Generator of uniformly distributed random numbers.
      * @param n Domain of the permutation.
@@ -86,6 +88,8 @@ public class PermutationSampler {
      * The {@code start} and {@code towardHead} parameters select which part
      * of the array is randomized and which is left untouched.
      *
+     * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
+     *
      * @param rng Random number generator.
      * @param list Array whose entries will be shuffled (in-place).
      * @param start Index at which shuffling begins.
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/SubsetSamplerUtils.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/SubsetSamplerUtils.java
index 32d58a1..0ac25f0 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/SubsetSamplerUtils.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/SubsetSamplerUtils.java
@@ -31,7 +31,7 @@ final class SubsetSamplerUtils {
      * Checks the subset of length {@code k} from {@code n} is valid.
      *
      * <p>If {@code n <= 0} or {@code k <= 0} or {@code k > n} then no subset
-     * is required and an exception is raised.
+     * is required and an exception is raised.</p>
      *
      * @param n   Size of the set.
      * @param k   Size of the subset.
@@ -57,7 +57,9 @@ final class SubsetSamplerUtils {
      * shuffled section.
      *
      * <p>The returned combination will have a length of {@code steps} for
-     * {@code upper=true}, or {@code domain.length - steps} otherwise.
+     * {@code upper=true}, or {@code domain.length - steps} otherwise.</p>
+     *
+     * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
      *
      * @param domain The domain.
      * @param steps  The number of shuffle steps.
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java
index 3dbdb03..661702b 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java
@@ -25,6 +25,13 @@ import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSa
  * Generate vectors <a href="http://mathworld.wolfram.com/SpherePointPicking.html">
  * isotropically located on the surface of a sphere</a>.
  *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextLong()}
+ *   <li>{@link UniformRandomProvider#nextDouble()}
+ * </ul>
+ *
  * @since 1.1
  */
 public class UnitSphereSampler {
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.java
index 56e57b4..3f8a2e7 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.java
@@ -21,6 +21,8 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Sampling from an <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">exponential distribution</a>.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  */
 public class AhrensDieterExponentialSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
index 946475b..bb17d54 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
@@ -39,6 +39,13 @@ import org.apache.commons.rng.UniformRandomProvider;
  *  </li>
  * </ul>
  *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextDouble()} (both algorithms)
+ *   <li>{@link UniformRandomProvider#nextLong()} (only for {@code theta >= 1})
+ * </ul>
+ *
  * @since 1.0
  */
 public class AhrensDieterMarsagliaTsangGammaSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerGaussianSampler.java
index c04f09a..b140823 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerGaussianSampler.java
@@ -22,6 +22,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * <a href="https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform">
  * Box-Muller algorithm</a> for sampling from a Gaussian distribution.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  *
  * @deprecated Since version 1.1. Please use {@link BoxMullerNormalizedGaussianSampler}
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
index fba683e..de8fc29 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
@@ -23,6 +23,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * log-normal distribution</a>.
  * Uses {@link BoxMullerNormalizedGaussianSampler} as the underlying sampler.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  *
  * @deprecated Since version 1.1. Please use {@link LogNormalSampler} instead.
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.java
index 78a6b74..49824f2 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.java
@@ -23,6 +23,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * Box-Muller algorithm</a> for sampling from Gaussian distribution with
  * mean 0 and standard deviation 1.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.1
  */
 public class BoxMullerNormalizedGaussianSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ChengBetaSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ChengBetaSampler.java
index 8ed0e3e..fd68958 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ChengBetaSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ChengBetaSampler.java
@@ -29,6 +29,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * </pre>
  * </blockquote>
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  */
 public class ChengBetaSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSampler.java
index 2c972f8..e7d749e 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ContinuousUniformSampler.java
@@ -21,6 +21,8 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Sampling from a uniform distribution.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  */
 public class ContinuousUniformSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java
index 00e308b..2b5ea52 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java
@@ -22,6 +22,10 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Discrete uniform distribution sampler.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)} when
+ * the range {@code (upper - lower) <} {@link Integer#MAX_VALUE}, otherwise
+ * {@link UniformRandomProvider#nextInt()}.</p>
+ *
  * @since 1.0
  */
 public class DiscreteUniformSampler
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 92b8df2..f4683ac 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
@@ -23,19 +23,21 @@ import org.apache.commons.rng.UniformRandomProvider;
  * distribution</a>.
  *
  * <p>This distribution samples the number of failures before the first success taking values in the
- * set {@code [0, 1, 2, ...]}.
+ * set {@code [0, 1, 2, ...]}.</p>
  *
  * <p>The sample is computed using a related exponential distribution. If \( X \) is an
  * exponentially distributed random variable with parameter \( \lambda \), then
  * \( Y = \left \lfloor X \right \rfloor \) is a geometrically distributed random variable with
- * parameter \( p = 1 − e^\lambda \), with \( p \) the probability of success.
+ * parameter \( p = 1 − e^\lambda \), with \( p \) the probability of success.</p>
  *
  * <p>This sampler outperforms using the {@link InverseTransformDiscreteSampler} with an appropriate
- * Geometric inverse cumulative probability function.
+ * Geometric inverse cumulative probability function.</p>
  *
  * <p>Usage note: As the probability of success (\( p \)) tends towards zero the mean of the
  * distribution (\( \frac{1-p}{p} \)) tends towards infinity and due to the use of {@code int}
- * for the sample this can result in truncation of the distribution.
+ * for the sample this can result in truncation of the distribution.</p>
+ *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
  *
  * @see <a
  * href="https://en.wikipedia.org/wiki/Geometric_distribution#Related_distributions">Geometric
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformContinuousSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformContinuousSampler.java
index ccaa138..5ba32fd 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformContinuousSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformContinuousSampler.java
@@ -24,7 +24,9 @@ import org.apache.commons.rng.UniformRandomProvider;
  * inversion method</a>.
  *
  * It can be used to sample any distribution that provides access to its
- * <em>inverse cumulative probabilty function</em>.
+ * <em>inverse cumulative probability function</em>.
+ *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
  *
  * <p>Example:</p>
  * <pre><code>
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformDiscreteSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformDiscreteSampler.java
index 5c43b58..276fb8e 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformDiscreteSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformDiscreteSampler.java
@@ -24,7 +24,9 @@ import org.apache.commons.rng.UniformRandomProvider;
  * inversion method</a>.
  *
  * It can be used to sample any distribution that provides access to its
- * <em>inverse cumulative probabilty function</em>.
+ * <em>inverse cumulative probability function</em>.
+ *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
  *
  * <p>Example:</p>
  * <pre><code>
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformParetoSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformParetoSampler.java
index 3b59483..4fd619b 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformParetoSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InverseTransformParetoSampler.java
@@ -21,6 +21,8 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Sampling from a <a href="https://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a>.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  */
 public class InverseTransformParetoSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.java
index 47ecab7..52af0ed 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.java
@@ -32,9 +32,16 @@ import org.apache.commons.rng.sampling.distribution.InternalUtils.FactorialLog;
  *  </li>
  * </ul>
  *
- * @since 1.1
+ * <p>This sampler is suitable for {@code mean >= 40}.</p>
+ *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextLong()}
+ *   <li>{@link UniformRandomProvider#nextDouble()}
+ * </ul>
  *
- * This sampler is suitable for {@code mean >= 40}.
+ * @since 1.1
  */
 public class LargeMeanPoissonSampler
     implements DiscreteSampler {
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
index 4b64461..af50b55 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
@@ -25,6 +25,8 @@ import org.apache.commons.rng.UniformRandomProvider;
  * This is a variation of the algorithm implemented in
  * {@link BoxMullerNormalizedGaussianSampler}.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.1
  */
 public class MarsagliaNormalizedGaussianSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
index 2889108..599e1cd 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
@@ -36,6 +36,13 @@ import org.apache.commons.rng.UniformRandomProvider;
  *  </li>
  * </ul>
  *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextDouble()}
+ *   <li>{@link UniformRandomProvider#nextLong()} (large means only)
+ * </ul>
+ *
  * @since 1.0
  */
 public class PoissonSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java
index 130feac..61aa73b 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java
@@ -25,14 +25,14 @@ import org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler.Larg
  * distribution</a> using a cache to minimise construction cost.
  *
  * <p>The cache will return a sampler equivalent to
- * {@link PoissonSampler#PoissonSampler(UniformRandomProvider, double)}.
+ * {@link PoissonSampler#PoissonSampler(UniformRandomProvider, double)}.</p>
  *
  * <p>The cache allows the {@link PoissonSampler} construction cost to be minimised
  * for low size Poisson samples. The cache stores state for a range of integers where
  * integer value {@code n} can be used to construct a sampler for the range
- * {@code n <= mean < n+1}.
+ * {@code n <= mean < n+1}.</p>
  *
- * <p>The cache is advantageous under the following conditions:
+ * <p>The cache is advantageous under the following conditions:</p>
  *
  * <ul>
  *   <li>The mean of the Poisson distribution falls within a known range.
@@ -44,15 +44,22 @@ import org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler.Larg
  *
  * <p>If the sample size to be made with the <strong>same</strong> sampler is large
  * then the construction cost is low compared to the sampling time and the cache
- * has minimal benefit.
+ * has minimal benefit.</p>
  *
  * <p>Performance improvement is dependent on the speed of the
  * {@link UniformRandomProvider}. A fast provider can obtain a two-fold speed
- * improvement for a single-use Poisson sampler.
+ * improvement for a single-use Poisson sampler.</p>
  *
  * <p>The cache is thread safe. Note that concurrent threads using the cache
  * must ensure a thread safe {@link UniformRandomProvider} is used when creating
- * samplers, e.g. a unique sampler per thread.
+ * samplers, e.g. a unique sampler per thread.</p>
+ *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextDouble()}
+ *   <li>{@link UniformRandomProvider#nextLong()} (large means only)
+ * </ul>
  *
  * @since 1.2
  */
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java
index 2e25934..d948c4b 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java
@@ -22,6 +22,8 @@ import org.apache.commons.rng.UniformRandomProvider;
 /**
  * Implementation of the <a href="https://en.wikipedia.org/wiki/Zipf's_law">Zipf distribution</a>.
  *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
+ *
  * @since 1.0
  */
 public class RejectionInversionZipfSampler
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.java
index 15a0d69..607feef 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.java
@@ -29,10 +29,12 @@ import org.apache.commons.rng.UniformRandomProvider;
  *  </li>
  * </ul>
  *
- * @since 1.1
+ * <p>This sampler is suitable for {@code mean < 40}.
+ * For large means, {@link LargeMeanPoissonSampler} should be used instead.</p>
+ *
+ * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
  *
- * This sampler is suitable for {@code mean < 40}.
- * For large means, {@link LargeMeanPoissonSampler} should be used instead.
+ * @since 1.1
  */
 public class SmallMeanPoissonSampler
     implements DiscreteSampler {
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java
index cbd5e06..bd7345d 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java
@@ -24,9 +24,16 @@ import org.apache.commons.rng.UniformRandomProvider;
  * Marsaglia and Tsang "Ziggurat" method</a> for sampling from a Gaussian
  * distribution with mean 0 and standard deviation 1.
  *
- * The algorithm is explained in this
+ * <p>The algorithm is explained in this
  * <a href="http://www.jstatsoft.org/article/view/v005i08/ziggurat.pdf">paper</a>
- * and this implementation has been adapted from the C code provided therein.
+ * and this implementation has been adapted from the C code provided therein.</p>
+ *
+ * <p>Sampling uses:</p>
+ *
+ * <ul>
+ *   <li>{@link UniformRandomProvider#nextLong()}
+ *   <li>{@link UniformRandomProvider#nextDouble()}
+ * </ul>
  *
  * @since 1.1
  */


[commons-rng] 02/10: RNG-79: Benchmark methods for producing nextDouble and nextFloat

Posted by ah...@apache.org.
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 ac929bf427059c957ace0ee880d6b6801d153601
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Mar 4 21:40:17 2019 +0000

    RNG-79: Benchmark methods for producing nextDouble and nextFloat
---
 .../commons/rng/core/util/NumberFactoryTest.java   |  63 +++++----
 .../rng/examples/jmh/NextDoublePerformance.java    | 152 +++++++++++++++++++++
 2 files changed, 189 insertions(+), 26 deletions(-)

diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
index 3088ca4..40e2989 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
@@ -29,29 +29,12 @@ public class NumberFactoryTest {
     final int LONG_SIZE = 8;
 
     /** Test values. */
-    private static final long[] LONG_TEST_VALUES = new long[] {
-        0L,
-        1L,
-        -1L,
-        19337L,
-        1234567891011213L,
-        -11109876543211L,
-        Long.valueOf(Integer.MAX_VALUE),
-        Long.valueOf(Integer.MIN_VALUE),
-        Long.MAX_VALUE,
-        Long.MIN_VALUE,
-    };
+    private static final long[] LONG_TEST_VALUES = new long[] { 0L, 1L, -1L, 19337L, 1234567891011213L,
+            -11109876543211L, Long.valueOf(Integer.MAX_VALUE), Long.valueOf(Integer.MIN_VALUE), Long.MAX_VALUE,
+            Long.MIN_VALUE, };
     /** Test values. */
-    private static final int[] INT_TEST_VALUES = new int[] {
-        0,
-        1,
-        -1,
-        19337,
-        1234567891,
-        -1110987656,
-        Integer.MAX_VALUE,
-        Integer.MIN_VALUE,
-    };
+    private static final int[] INT_TEST_VALUES = new int[] { 0, 1, -1, 19337, 1234567891, -1110987656,
+            Integer.MAX_VALUE, Integer.MIN_VALUE, };
 
     @Test
     public void testMakeIntFromLong() {
@@ -85,8 +68,7 @@ public class NumberFactoryTest {
     @Test
     public void testLongArrayFromByteArray2LongArray() {
         final byte[] b = NumberFactory.makeByteArray(LONG_TEST_VALUES);
-        Assert.assertArrayEquals(LONG_TEST_VALUES,
-                                 NumberFactory.makeLongArray(b));
+        Assert.assertArrayEquals(LONG_TEST_VALUES, NumberFactory.makeLongArray(b));
     }
 
     @Test
@@ -100,8 +82,7 @@ public class NumberFactoryTest {
     @Test
     public void testIntArrayFromByteArray2IntArray() {
         final byte[] b = NumberFactory.makeByteArray(INT_TEST_VALUES);
-        Assert.assertArrayEquals(INT_TEST_VALUES,
-                                 NumberFactory.makeIntArray(b));
+        Assert.assertArrayEquals(INT_TEST_VALUES, NumberFactory.makeIntArray(b));
     }
 
     @Test
@@ -159,4 +140,34 @@ public class NumberFactoryTest {
             }
         }
     }
+
+    @Test
+    public void testFloatGeneration() {
+        final int allBits = 0xffffffff;
+
+        // Not capable of generating 1
+        Assert.assertEquals(1, (allBits >>> 9) * 0x1.0p-23f, 1e-6);
+        Assert.assertEquals(1, (allBits >>> 8) * 0x1.0p-24f, 1e-6);
+        Assert.assertEquals(1, Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, 1e-6);
+
+        final int noBits = 0;
+        Assert.assertEquals(0, (noBits >>> 8) * 0x1.0p-24f, 0);
+        Assert.assertEquals(0, (noBits >>> 9) * 0x1.0p-23f, 0);
+        Assert.assertEquals(0, Float.intBitsToFloat(0x7f << 23 | noBits >>> 9) - 1.0f, 0);
+    }
+
+    @Test
+    public void testDoubleGeneration() {
+        final long allBits = 0xffffffffffffffffL;
+
+        // Not capable of generating 1
+        Assert.assertEquals(1, (allBits >>> 12) * 0x1.0p-52d, 1e-10);
+        Assert.assertEquals(1, (allBits >>> 11) * 0x1.0p-53d, 1e-10);
+        Assert.assertEquals(1, Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, 1e-10);
+
+        final long noBits = 0;
+        Assert.assertEquals(0, (noBits >>> 12) * 0x1.0p-52d, 0);
+        Assert.assertEquals(0, (noBits >>> 11) * 0x1.0p-53d, 0);
+        Assert.assertEquals(0, Double.longBitsToDouble(0x3ffL << 52 | noBits >>> 12) - 1.0, 0);
+    }
 }
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java
new file mode 100644
index 0000000..f182955
--- /dev/null
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rng.examples.jmh;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Executes benchmark to compare the speed of generation of floating point
+ * numbers from the integer primitives.
+ */
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MICROSECONDS)
+@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = { "-server", "-Xms128M", "-Xmx128M" })
+public class NextDoublePerformance {
+    // Mimic the generation of the SplitMix64 algorithm
+    // and a SplitMix32 algorithm to get a spread of input numbers.
+
+    /**
+     * The 64-bit golden ratio number.
+     */
+    private static final long GOLDEN_64 = 0x9e3779b97f4a7c15L;
+    /**
+     * The 32-bit golden ratio number.
+     */
+    private static final long GOLDEN_32 = 0x9e3779b9;
+
+    /** The long state. */
+    private long longState = ThreadLocalRandom.current().nextLong();
+    /** The int state. */
+    private int intState = ThreadLocalRandom.current().nextInt();
+
+    /**
+     * Get the next long in the sequence.
+     *
+     * @return the long
+     */
+    private long nextLong() {
+        return longState += GOLDEN_64;
+    }
+
+    /**
+     * Get the next int in the sequence.
+     *
+     * @return the int
+     */
+    private int nextInt() {
+        return intState += GOLDEN_32;
+    }
+
+    // Benchmark methods
+
+    /**
+     * @return the long
+     */
+    @Benchmark
+    public long nextDoubleBaseline() {
+        return nextLong();
+    }
+
+    /**
+     * @return the double
+     */
+    @Benchmark
+    public double nextDoubleUsingBitsToDouble() {
+        // Combine 11 bit unsigned exponent with value 1023 (768 + 255) with 52 bit mantissa
+        // 0x300L = 256 + 512 = 768
+        // 0x0ff  = 255
+        // This makes a number in the range 1.0 to 2.0 so subtract 1.0
+        return Double.longBitsToDouble(0x3ffL << 52 | nextLong() >>> 12) - 1.0;
+    }
+
+    /**
+     * @return the double
+     */
+    @Benchmark
+    public double nextDoubleUsingMultiply52bits() {
+        return (nextLong() >>> 12) * 0x1.0p-52d; // 1.0 / (1L << 52)
+    }
+
+    /**
+     * @return the double
+     */
+    @Benchmark
+    public double nextDoubleUsingMultiply53bits() {
+        return (nextLong() >>> 11) * 0x1.0p-53d; // 1.0 / (1L << 53)
+    }
+
+    /**
+     * @return the int
+     */
+    @Benchmark
+    public int nextFloatBaseline() {
+        return nextInt();
+    }
+
+    /**
+     * @return the float
+     */
+    @Benchmark
+    public float nextFloatUsingBitsToFloat() {
+        // Combine 8 bit unsigned exponent with value 127 (112 + 15) with 23 bit mantissa
+        // 0x70 = 64 + 32 + 16 = 112
+        // 0x0f = 15
+        // This makes a number in the range 1.0f to 2.0f so subtract 1.0f
+        return Float.intBitsToFloat(0x7f << 23 | nextInt() >>> 9) - 1.0f;
+    }
+
+    /**
+     * @return the float
+     */
+    @Benchmark
+    public float nextFloatUsingMultiply23bits() {
+        return (nextInt() >>> 9) * 0x1.0p-23f; // 1.0f / (1 << 23)
+    }
+
+    /**
+     * @return the float
+     */
+    @Benchmark
+    public float nextFloatUsingMultiply24bits() {
+        return (nextInt() >>> 8) * 0x1.0p-24f; // 1.0f / (1 << 24)
+    }
+}


[commons-rng] 06/10: RNG-79: Modify benchmark to use uniform random generator.

Posted by ah...@apache.org.
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 80c94a5916041fef587796abcafe646387c461c0
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Mar 5 12:25:14 2019 +0000

    RNG-79: Modify benchmark to use uniform random generator.
---
 .../rng/examples/jmh/NextDoublePerformance.java    | 102 +++++++++++----------
 1 file changed, 56 insertions(+), 46 deletions(-)

diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java
index f182955..70620f3 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/NextDoublePerformance.java
@@ -41,112 +41,122 @@ import java.util.concurrent.TimeUnit;
 @State(Scope.Benchmark)
 @Fork(value = 1, jvmArgs = { "-server", "-Xms128M", "-Xmx128M" })
 public class NextDoublePerformance {
-    // Mimic the generation of the SplitMix64 algorithm
-    // and a SplitMix32 algorithm to get a spread of input numbers.
-
-    /**
-     * The 64-bit golden ratio number.
-     */
-    private static final long GOLDEN_64 = 0x9e3779b97f4a7c15L;
-    /**
-     * The 32-bit golden ratio number.
-     */
-    private static final long GOLDEN_32 = 0x9e3779b9;
-
-    /** The long state. */
-    private long longState = ThreadLocalRandom.current().nextLong();
-    /** The int state. */
-    private int intState = ThreadLocalRandom.current().nextInt();
-
     /**
-     * Get the next long in the sequence.
+     * Mimic the generation of the SplitMix64 algorithm.
      *
-     * @return the long
-     */
-    private long nextLong() {
-        return longState += GOLDEN_64;
-    }
-
-    /**
-     * Get the next int in the sequence.
-     *
-     * @return the int
+     * <p>The final mixing step must be included otherwise the output numbers are sequential
+     * and the test may run with a lack of numbers with higher order bits.
      */
-    private int nextInt() {
-        return intState += GOLDEN_32;
+    @State(Scope.Benchmark)
+    public static class LongSource {
+        /** The state. */
+        private long state = ThreadLocalRandom.current().nextLong();
+
+        /**
+         * Get the next long.
+         *
+         * @return the long
+         */
+        public final long nextLong() {
+            long z = state += 0x9e3779b97f4a7c15L;
+            z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
+            z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
+            return z ^ (z >>> 31);
+        }
+
+        /**
+         * Get the next int.
+         *
+         * <p>Returns the 32 high bits of Stafford variant 4 mix64 function as int.
+         *
+         * @return the int
+         */
+        public final int nextInt() {
+            long z = state += 0x9e3779b97f4a7c15L;
+            z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
+            return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
+        }
     }
 
     // Benchmark methods
 
     /**
+     * @param source the source
      * @return the long
      */
     @Benchmark
-    public long nextDoubleBaseline() {
-        return nextLong();
+    public long nextDoubleBaseline(LongSource source) {
+        return source.nextLong();
     }
 
     /**
+     * @param source the source
      * @return the double
      */
     @Benchmark
-    public double nextDoubleUsingBitsToDouble() {
+    public double nextDoubleUsingBitsToDouble(LongSource source) {
         // Combine 11 bit unsigned exponent with value 1023 (768 + 255) with 52 bit mantissa
         // 0x300L = 256 + 512 = 768
         // 0x0ff  = 255
         // This makes a number in the range 1.0 to 2.0 so subtract 1.0
-        return Double.longBitsToDouble(0x3ffL << 52 | nextLong() >>> 12) - 1.0;
+        return Double.longBitsToDouble(0x3ffL << 52 | source.nextLong() >>> 12) - 1.0;
     }
 
     /**
+     * @param source the source
      * @return the double
      */
     @Benchmark
-    public double nextDoubleUsingMultiply52bits() {
-        return (nextLong() >>> 12) * 0x1.0p-52d; // 1.0 / (1L << 52)
+    public double nextDoubleUsingMultiply52bits(LongSource source) {
+        return (source.nextLong() >>> 12) * 0x1.0p-52d; // 1.0 / (1L << 52)
     }
 
     /**
+     * @param source the source
      * @return the double
      */
     @Benchmark
-    public double nextDoubleUsingMultiply53bits() {
-        return (nextLong() >>> 11) * 0x1.0p-53d; // 1.0 / (1L << 53)
+    public double nextDoubleUsingMultiply53bits(LongSource source) {
+        return (source.nextLong() >>> 11) * 0x1.0p-53d; // 1.0 / (1L << 53)
     }
 
     /**
+     * @param source the source
      * @return the int
      */
     @Benchmark
-    public int nextFloatBaseline() {
-        return nextInt();
+    public int nextFloatBaseline(LongSource source) {
+        return source.nextInt();
     }
 
     /**
+     * @param source the source
      * @return the float
      */
     @Benchmark
-    public float nextFloatUsingBitsToFloat() {
+    public float nextFloatUsingBitsToFloat(LongSource source) {
         // Combine 8 bit unsigned exponent with value 127 (112 + 15) with 23 bit mantissa
         // 0x70 = 64 + 32 + 16 = 112
         // 0x0f = 15
         // This makes a number in the range 1.0f to 2.0f so subtract 1.0f
-        return Float.intBitsToFloat(0x7f << 23 | nextInt() >>> 9) - 1.0f;
+        return Float.intBitsToFloat(0x7f << 23 | source.nextInt() >>> 9) - 1.0f;
     }
 
     /**
+     * @param source the source
      * @return the float
      */
     @Benchmark
-    public float nextFloatUsingMultiply23bits() {
-        return (nextInt() >>> 9) * 0x1.0p-23f; // 1.0f / (1 << 23)
+    public float nextFloatUsingMultiply23bits(LongSource source) {
+        return (source.nextInt() >>> 9) * 0x1.0p-23f; // 1.0f / (1 << 23)
     }
 
     /**
+     * @param source the source
      * @return the float
      */
     @Benchmark
-    public float nextFloatUsingMultiply24bits() {
-        return (nextInt() >>> 8) * 0x1.0p-24f; // 1.0f / (1 << 24)
+    public float nextFloatUsingMultiply24bits(LongSource source) {
+        return (source.nextInt() >>> 8) * 0x1.0p-24f; // 1.0f / (1 << 24)
     }
 }


[commons-rng] 09/10: Merge branch 'feature-RNG-79'

Posted by ah...@apache.org.
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 526274452b2ce4a00d8df7ebbe19825709f5bb93
Merge: bfef2b6 80c94a5
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Mar 6 15:44:48 2019 +0000

    Merge branch 'feature-RNG-79'
    
    Closes #28

 commons-rng-core/pom.xml                           |   7 +
 .../commons/rng/core/util/NumberFactoryTest.java   | 103 +++++++++----
 .../rng/examples/jmh/NextDoublePerformance.java    | 162 +++++++++++++++++++++
 3 files changed, 246 insertions(+), 26 deletions(-)


[commons-rng] 05/10: RNG-79: NumberFactoryTest floating-point precision uses commons-math3

Posted by ah...@apache.org.
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 d4950ef16cc79c86c0ad48129009f6f0bacbeadf
Author: aherbert <ah...@apache.org>
AuthorDate: Tue Mar 5 11:22:12 2019 +0000

    RNG-79: NumberFactoryTest floating-point precision uses commons-math3
---
 commons-rng-core/pom.xml                           |  7 +++
 .../commons/rng/core/util/NumberFactoryTest.java   | 60 +++++++++++++++++-----
 2 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/commons-rng-core/pom.xml b/commons-rng-core/pom.xml
index ceeaf48..e027ec6 100644
--- a/commons-rng-core/pom.xml
+++ b/commons-rng-core/pom.xml
@@ -53,6 +53,13 @@
       <artifactId>commons-rng-client-api</artifactId>
       <version>1.3-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <!-- For testing floating point precision -->
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-math3</artifactId>
+      <version>3.6.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
index 6fb909e..9bbcc4d 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.rng.core.util;
 
+import org.apache.commons.math3.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -141,16 +142,18 @@ public class NumberFactoryTest {
         }
     }
 
+    /**
+     * Test different methods for generation of a {@code float} from a {@code int}. The output
+     * value should be in the range between 0 and 1.
+     */
     @Test
-    public void testFloatGeneration() {
+    public void testFloatGenerationMethods() {
         final int allBits = 0xffffffff;
 
         // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1.
-        final float deltaUlp2 = 1f - Math.nextAfter(Math.nextAfter(1f, -1), -1);
-        final float deltaUlp1 = 1f - Math.nextAfter(1f, -1);
-        Assert.assertEquals(1, (allBits >>> 9) * 0x1.0p-23f, deltaUlp2);
-        Assert.assertEquals(1, (allBits >>> 8) * 0x1.0p-24f, deltaUlp1);
-        Assert.assertEquals(1, Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, deltaUlp2);
+        assertCloseToNotAbove1((allBits >>> 9) * 0x1.0p-23f, 2);
+        assertCloseToNotAbove1((allBits >>> 8) * 0x1.0p-24f, 1);
+        assertCloseToNotAbove1(Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, 2);
 
         final int noBits = 0;
         Assert.assertEquals(0, (noBits >>> 9) * 0x1.0p-23f, 0);
@@ -158,20 +161,53 @@ public class NumberFactoryTest {
         Assert.assertEquals(0, Float.intBitsToFloat(0x7f << 23 | noBits >>> 9) - 1.0f, 0);
     }
 
+    /**
+     * Test different methods for generation of a {@code double} from a {@code long}. The output
+     * value should be in the range between 0 and 1.
+     */
     @Test
-    public void testDoubleGeneration() {
+    public void testDoubleGenerationMethods() {
         final long allBits = 0xffffffffffffffffL;
 
         // Not capable of generating 1. Set the delta with 1 or 2 ULP of 1.
-        final double deltaUlp2 = 1 - Math.nextAfter(Math.nextAfter(1, -1), -1);
-        final double deltaUlp1 = 1 - Math.nextAfter(1, -1);
-        Assert.assertEquals(1, (allBits >>> 12) * 0x1.0p-52d, deltaUlp2);
-        Assert.assertEquals(1, (allBits >>> 11) * 0x1.0p-53d, deltaUlp1);
-        Assert.assertEquals(1, Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, deltaUlp2);
+        assertCloseToNotAbove1((allBits >>> 12) * 0x1.0p-52d, 2);
+        assertCloseToNotAbove1((allBits >>> 11) * 0x1.0p-53d, 1);
+        assertCloseToNotAbove1(Double.longBitsToDouble(0x3ffL << 52 | allBits >>> 12) - 1.0, 2);
 
         final long noBits = 0;
         Assert.assertEquals(0, (noBits >>> 12) * 0x1.0p-52d, 0);
         Assert.assertEquals(0, (noBits >>> 11) * 0x1.0p-53d, 0);
         Assert.assertEquals(0, Double.longBitsToDouble(0x3ffL << 52 | noBits >>> 12) - 1.0, 0);
     }
+
+
+    /**
+     * Assert that the value is close to but <strong>not above</strong> 1. This is used to test
+     * the output from methods that produce a {@code float} value that must be in the range
+     * between 0 and 1.
+     *
+     * @param value the value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point values between x and y.
+     * @see Precision#equals(float, float, int)
+     */
+    private static void assertCloseToNotAbove1(float value, int maxUlps) {
+        Assert.assertTrue("Not <= 1.0f", value <= 1.0f);
+        Assert.assertTrue("Not equal to 1.0f within units of least precision: " + maxUlps,
+                          Precision.equals(1.0f, value, maxUlps));
+    }
+
+    /**
+     * Assert that the value is close to but <strong>not above</strong> 1. This is used to test
+     * the output from methods that produce a {@code double} value that must be in the range
+     * between 0 and 1.
+     *
+     * @param value the value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point values between x and y.
+     * @see Precision#equals(double, double, int)
+     */
+    private static void assertCloseToNotAbove1(double value, int maxUlps) {
+        Assert.assertTrue("Not <= 1.0", value <= 1.0);
+        Assert.assertTrue("Not equal to 1.0 within units of least precision: " + maxUlps,
+                          Precision.equals(1.0, value, maxUlps));
+    }
 }


[commons-rng] 03/10: RNG-79: Updated testFloatGeneration internal order

Posted by ah...@apache.org.
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 d03726b337653c430789cd18f3d5b9a5d7df21be
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Mar 4 21:48:55 2019 +0000

    RNG-79: Updated testFloatGeneration internal order
---
 .../test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
index 40e2989..5638768 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java
@@ -151,8 +151,8 @@ public class NumberFactoryTest {
         Assert.assertEquals(1, Float.intBitsToFloat(0x7f << 23 | allBits >>> 9) - 1.0f, 1e-6);
 
         final int noBits = 0;
-        Assert.assertEquals(0, (noBits >>> 8) * 0x1.0p-24f, 0);
         Assert.assertEquals(0, (noBits >>> 9) * 0x1.0p-23f, 0);
+        Assert.assertEquals(0, (noBits >>> 8) * 0x1.0p-24f, 0);
         Assert.assertEquals(0, Float.intBitsToFloat(0x7f << 23 | noBits >>> 9) - 1.0f, 0);
     }
 


[commons-rng] 08/10: Track changes.

Posted by ah...@apache.org.
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 bfef2b672a9a21961c75cb024582a2fe5cbb261d
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Mar 6 15:43:35 2019 +0000

    Track changes.
---
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e49e200..0f824fb 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -75,6 +75,9 @@ re-run tests that fail, and pass the build if they succeed
 within the allotted number of reruns (the test will be marked
 as 'flaky' in the report).
 ">
+      <action dev="aherbert" type="update" issue="RNG-73">
+        Add the methods used from UniformRandomProvider to each sampler in the sampling module.
+      </action>
       <action dev="aherbert" type="update" issue="RNG-74">
         "DiscreteUniformSampler": Algorithms for small and large integer ranges have
         been delegated to specialised classes.


[commons-rng] 10/10: Track changes.

Posted by ah...@apache.org.
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 f5ac62537baafcbad749bbda29bd8600ce53c3aa
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Mar 6 15:46:16 2019 +0000

    Track changes.
---
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0f824fb..0e2d57f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -75,6 +75,9 @@ re-run tests that fail, and pass the build if they succeed
 within the allotted number of reruns (the test will be marked
 as 'flaky' in the report).
 ">
+      <action dev="aherbert" type="add" issue="RNG-79">
+        Benchmark methods for producing nextDouble and nextFloat.
+      </action>
       <action dev="aherbert" type="update" issue="RNG-73">
         Add the methods used from UniformRandomProvider to each sampler in the sampling module.
       </action>


[commons-rng] 07/10: Merge branch 'improvement-RNG-73'

Posted by ah...@apache.org.
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 cf51f2c28a48bdb1d5c4fd98fdd85f63392d8911
Merge: fccc73e fbeeb08
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Mar 6 15:41:19 2019 +0000

    Merge branch 'improvement-RNG-73'
    
    Closes #25

 .../commons/rng/sampling/CollectionSampler.java       |  2 ++
 .../commons/rng/sampling/CombinationSampler.java      |  8 +++++---
 .../DiscreteProbabilityCollectionSampler.java         |  2 ++
 .../org/apache/commons/rng/sampling/ListSampler.java  | 10 +++++++++-
 .../commons/rng/sampling/PermutationSampler.java      |  8 ++++++--
 .../commons/rng/sampling/SubsetSamplerUtils.java      |  6 ++++--
 .../commons/rng/sampling/UnitSphereSampler.java       |  7 +++++++
 .../distribution/AhrensDieterExponentialSampler.java  |  2 ++
 .../AhrensDieterMarsagliaTsangGammaSampler.java       |  7 +++++++
 .../distribution/BoxMullerGaussianSampler.java        |  2 ++
 .../distribution/BoxMullerLogNormalSampler.java       |  2 ++
 .../BoxMullerNormalizedGaussianSampler.java           |  2 ++
 .../rng/sampling/distribution/ChengBetaSampler.java   |  2 ++
 .../distribution/ContinuousUniformSampler.java        |  2 ++
 .../sampling/distribution/DiscreteUniformSampler.java |  4 ++++
 .../rng/sampling/distribution/GeometricSampler.java   | 10 ++++++----
 .../InverseTransformContinuousSampler.java            |  4 +++-
 .../distribution/InverseTransformDiscreteSampler.java |  4 +++-
 .../distribution/InverseTransformParetoSampler.java   |  2 ++
 .../distribution/LargeMeanPoissonSampler.java         | 11 +++++++++--
 .../MarsagliaNormalizedGaussianSampler.java           |  2 ++
 .../rng/sampling/distribution/PoissonSampler.java     |  7 +++++++
 .../sampling/distribution/PoissonSamplerCache.java    | 19 +++++++++++++------
 .../distribution/RejectionInversionZipfSampler.java   |  2 ++
 .../distribution/SmallMeanPoissonSampler.java         |  8 +++++---
 .../ZigguratNormalizedGaussianSampler.java            | 11 +++++++++--
 26 files changed, 119 insertions(+), 27 deletions(-)