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 ce151a2f2d02a0cecb304016660f45a06ad90a37
Author: aherbert <aherb...@apache.org>
AuthorDate: Fri Jul 9 09:19:03 2021 +0100

    Move method to make a double in (0, 1] to the sampling utils class.
---
 .../distribution/AhrensDieterExponentialSampler.java   | 18 +-----------------
 .../rng/sampling/distribution/InternalUtils.java       | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 17 deletions(-)

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 702ecf7..0909787 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
@@ -46,11 +46,6 @@ public class AhrensDieterExponentialSampler
      * By trying, n = 16 in Java is enough to reach 1.
      */
     private static final double[] EXPONENTIAL_SA_QI = new double[16];
-    /**
-     * The multiplier to convert the least significant 53-bits of a {@code 
long} to a {@code double}.
-     * Taken from org.apache.commons.rng.core.util.NumberFactory.
-     */
-    private static final double DOUBLE_MULTIPLIER = 0x1.0p-53d;
     /** The mean of this distribution. */
     private final double mean;
     /** Underlying source of randomness. */
@@ -105,7 +100,7 @@ public class AhrensDieterExponentialSampler
         // Step 1:
         double a = 0;
         // Avoid u=0 which creates an infinite loop
-        double u = nextNonZeroDouble();
+        double u = InternalUtils.makeNonZeroDouble(rng.nextLong());
 
         // Step 2 and 3:
         while (u < 0.5) {
@@ -141,17 +136,6 @@ public class AhrensDieterExponentialSampler
         return mean * (a + umin * EXPONENTIAL_SA_QI[0]);
     }
 
-    /**
-     * Create a double in the interval {@code (0, 1]}.
-     *
-     * @return a {@code double} value in the interval {@code (0, 1]}.
-     */
-    private double nextNonZeroDouble() {
-        // This matches the method in 
o.a.c.rng.core.util.NumberFactory.makeDouble(long)
-        // but shifts the range from [0, 1) to (0, 1].
-        return ((rng.nextLong() >>> 11) + 1L) * DOUBLE_MULTIPLIER;
-    }
-
     /** {@inheritDoc} */
     @Override
     public String toString() {
diff --git 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
index af4c049..64d3ddc 100644
--- 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
+++ 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
@@ -39,6 +39,12 @@ final class InternalUtils { // Class is package-private on 
purpose; do not make
     /** The first array index with a non-zero log factorial. */
     private static final int BEGIN_LOG_FACTORIALS = 2;
 
+    /**
+     * The multiplier to convert the least significant 53-bits of a {@code 
long} to a {@code double}.
+     * Taken from org.apache.commons.rng.core.util.NumberFactory.
+     */
+    private static final double DOUBLE_MULTIPLIER = 0x1.0p-53d;
+
     /** Utility class. */
     private InternalUtils() {}
 
@@ -119,6 +125,18 @@ final class InternalUtils { // Class is package-private on 
purpose; do not make
     }
 
     /**
+     * Creates a {@code double} in the interval {@code (0, 1]} from a {@code 
long} value.
+     *
+     * @param v Number.
+     * @return a {@code double} value in the interval {@code (0, 1]}.
+     */
+    static double makeNonZeroDouble(long v) {
+        // This matches the method in 
o.a.c.rng.core.util.NumberFactory.makeDouble(long)
+        // but shifts the range from [0, 1) to (0, 1].
+        return ((v >>> 11) + 1L) * DOUBLE_MULTIPLIER;
+    }
+
+    /**
      * Class for computing the natural logarithm of the factorial of {@code n}.
      * It allows to allocate a cache of precomputed values.
      * In case of cache miss, computation is performed by a call to

Reply via email to