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 2021/08/06 18:25:04 UTC

[commons-rng] branch master updated: Recycle bits in the sample method

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


The following commit(s) were added to refs/heads/master by this push:
     new 20d28ac  Recycle bits in the sample method
20d28ac is described below

commit 20d28aca63fcc5c978cf57f9fea2073961c75519
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Aug 6 19:25:00 2021 +0100

    Recycle bits in the sample method
    
    This change is an update to match the reference c implementation.
---
 .../sampling/distribution/ZigguratSamplerPerformance.java |  4 +++-
 .../rng/sampling/distribution/ZigguratSampler.java        | 15 ++++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
index 1757f08..9e9679c 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
@@ -960,7 +960,9 @@ public class ZigguratSamplerPerformance {
                 return X[i] * xx;
             }
 
-            long u1 = randomInt63();
+            // Recycle bits then advance RNG:
+            // u1 = RANDOM_INT63();
+            long u1 = xx & MAX_INT64;
             // Another squashed, recyclable bit
             // double sign_bit = u1 & 0x100 ? 1. : -1.
             // Use 2 - 1 or 0 - 1
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java
index 9e8833f..82c0ec7 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java
@@ -78,6 +78,12 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler {
     // available in Commons RNG the c and java code output the same random
     // deviates over 2^30 cycles if identically seeded. Branch frequencies have
     // been measured and added as comments.
+    //
+    // Note: The c implementation uses a RNG where the current value can be obtained
+    // without advancing the generator. The entry point to the sample generation
+    // always has this value as a previously unused value. The RNG is advanced when new
+    // bits are required. This Java implementation will generate new values with calls
+    // to the RNG and cache the value if it is to be recycled.
     // =========================================================================
 
     /**
@@ -675,7 +681,9 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler {
                 return X[i] * xx;
             }
 
-            long u1 = randomInt63();
+            // Recycle bits then advance RNG:
+            // u1 = RANDOM_INT63();
+            long u1 = xx & MAX_INT64;
             // Another squashed, recyclable bit
             // double sign_bit = u1 & 0x100 ? 1. : -1.
             // Use 2 - 1 or 0 - 1
@@ -811,6 +819,11 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler {
     /**
      * Generates a positive {@code long} in {@code [0, 2^63)}.
      *
+     * <p>In the c reference implementation RANDOM_INT63() obtains the current random value
+     * and then advances the RNG. This implementation obtains a new value from the RNG.
+     * Thus the java implementation must ensure a previous call to the RNG is cached
+     * if RANDOM_INT63() is called without first advancing the RNG.
+     *
      * @return the long
      */
     long randomInt63() {