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/10/02 21:13:05 UTC

[commons-rng] branch master updated: Add options to test the upper or lower 32-bits from nextLong().

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 b557dd0  Add options to test the upper or lower 32-bits from nextLong().
b557dd0 is described below

commit b557dd03a3c7d3fcaf3d1ba5f4cdd0c486168b6a
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Oct 2 22:13:01 2019 +0100

    Add options to test the upper or lower 32-bits from nextLong().
---
 .../commons/rng/examples/stress/RNGUtils.java      | 42 ++++++++++++++++++++++
 .../rng/examples/stress/StressTestCommand.java     | 19 +++++++++-
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RNGUtils.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RNGUtils.java
index a82a393..fc8ac47 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RNGUtils.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RNGUtils.java
@@ -88,6 +88,48 @@ final class RNGUtils {
     }
 
     /**
+     * Wrap the random generator with an {@link IntProvider} that will use the upper 32-bits
+     * of the {@code long} from {@link UniformRandomProvider#nextLong()}.
+     *
+     * @param rng The random generator.
+     * @return the upper bits random generator.
+     */
+    static UniformRandomProvider createLongUpperBitsIntProvider(final UniformRandomProvider rng) {
+        return new IntProvider() {
+            @Override
+            public int next() {
+                return (int) (rng.nextLong() >>> 32);
+            }
+
+            @Override
+            public String toString() {
+                return "Long upper-bits " + rng.toString();
+            }
+        };
+    }
+
+    /**
+     * Wrap the random generator with an {@link IntProvider} that will use the lower 32-bits
+     * of the {@code long} from {@link UniformRandomProvider#nextLong()}.
+     *
+     * @param rng The random generator.
+     * @return the lower bits random generator.
+     */
+    static UniformRandomProvider createLongLowerBitsIntProvider(final UniformRandomProvider rng) {
+        return new IntProvider() {
+            @Override
+            public int next() {
+                return (int) rng.nextLong();
+            }
+
+            @Override
+            public String toString() {
+                return "Long lower-bits " + rng.toString();
+            }
+        };
+    }
+
+    /**
      * Wrap the random generator with an {@link IntProvider} that will combine the bits
      * using a {@code xor} operation with a generated hash code.
      *
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
index 2bc87fd..aa02d34 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
@@ -132,9 +132,21 @@ class StressTestCommand implements Callable<Void> {
                            "when passing using the standard sequence."})
     private boolean reverseBits;
 
+    /** Flag to use the upper 32-bits from the 64-bit long output. */
+    @Option(names = {"--high-bits"},
+            description = {"Use the upper 32-bits from the 64-bit long output.",
+                           "Takes precedent over --low-bits."})
+    private boolean longHighBits;
+
+    /** Flag to use the lower 32-bits from the 64-bit long output. */
+    @Option(names = {"--low-bits"},
+            description = {"Use the lower 32-bits from the 64-bit long output."})
+    private boolean longLowBits;
+
     /**
      * Flag to indicate the output should be combined with a hashcode from a new object.
-     * This is a method used in the {@link org.apache.commons.rng.simple.internal.SeedFactory SeedFactory}.
+     * This is a method previously used in the
+     * {@link org.apache.commons.rng.simple.internal.SeedFactory SeedFactory}.
      *
      * @see System#identityHashCode(Object)
      */
@@ -445,6 +457,11 @@ class StressTestCommand implements Callable<Void> {
             final byte[] seed = testData.getRandomSource().createSeed();
             UniformRandomProvider rng = testData.createRNG(seed);
             // Combined generators must be created first
+            if (longHighBits) {
+                rng = RNGUtils.createLongUpperBitsIntProvider(rng);
+            } else if (longLowBits) {
+                rng = RNGUtils.createLongLowerBitsIntProvider(rng);
+            }
             if (xorHashCode) {
                 rng = RNGUtils.createHashCodeIntProvider(rng);
             }