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 7f95834  Add option to combine generator output with ThreadLocalRandom.
7f95834 is described below

commit 7f95834e3c45fd7a3e16db3bfb46a401551ac728
Author: aherbert <[email protected]>
AuthorDate: Tue Jun 4 15:39:21 2019 +0100

    Add option to combine generator output with ThreadLocalRandom.
---
 .../commons/rng/examples/stress/RNGUtils.java      | 29 +++++++++++++++++++++-
 .../rng/examples/stress/StressTestCommand.java     | 22 +++++++++++++---
 2 files changed, 46 insertions(+), 5 deletions(-)

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 a7f77d5..ab1ee17 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
@@ -19,6 +19,8 @@ package org.apache.commons.rng.examples.stress;
 import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.rng.core.source32.IntProvider;
 
+import java.util.concurrent.ThreadLocalRandom;
+
 /**
  * Utility methods for a {@link UniformRandomProvider}.
  */
@@ -89,7 +91,7 @@ final class RNGUtils {
      * Note: This generator will be slow.
      *
      * @param rng The random generator.
-     * @return the hash code combined random generator.
+     * @return the combined random generator.
      * @see System#identityHashCode(Object)
      */
     static UniformRandomProvider createHashCodeIntProvider(final 
UniformRandomProvider rng) {
@@ -107,6 +109,31 @@ final class RNGUtils {
     }
 
     /**
+     * Wrap the random generator with an {@link IntProvider} that will combine 
the bits
+     * using a {@code xor} operation with the output from {@link 
ThreadLocalRandom}.
+     *
+     * <pre>{@code
+     * ThreadLocalRandom.current().nextInt() ^ rng.nextInt()
+     * }</pre>
+     *
+     * @param rng The random generator.
+     * @return the combined random generator.
+     */
+    static UniformRandomProvider createThreadLocalRandomIntProvider(final 
UniformRandomProvider rng) {
+        return new IntProvider() {
+            @Override
+            public int next() {
+                return ThreadLocalRandom.current().nextInt() ^ rng.nextInt();
+            }
+
+            @Override
+            public String toString() {
+                return "ThreadLocalRandom ^ " + rng.toString();
+            }
+        };
+    }
+
+    /**
      * Parses the argument into an object suitable for the RandomSource 
constructor. Supports:
      *
      * <ul>
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 a34721e..7a6b20e 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
@@ -139,6 +139,15 @@ class StressTestCommand implements Callable<Void> {
                            "System.identityHashCode(new Object()) ^ 
rng.nextInt()."})
     private boolean xorHashCode;
 
+    /**
+     * Flag to indicate the output should be combined with output from 
ThreadLocalRandom.
+     *
+     * @see System#identityHashCode(Object)
+     */
+    @Option(names = {"--local-random"},
+            description = {"Combine the bits with ThreadLocalRandom (default: 
${DEFAULT-VALUE})."})
+    private boolean xorLocalRandom;
+
     /** The flag to indicate a dry run. */
     @Option(names = {"--dry-run"},
             description = "Perform a dry run where the generators and output 
files are created " +
@@ -402,14 +411,19 @@ class StressTestCommand implements Callable<Void> {
             }
             // Create the generator
             UniformRandomProvider rng = testData.createRNG();
-            if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) {
-                rng = RNGUtils.createReverseBytesIntProvider(rng);
+            // Combined generators must be created first
+            if (xorHashCode) {
+                rng = RNGUtils.createHashCodeIntProvider(rng);
+            }
+            if (xorLocalRandom) {
+                rng = RNGUtils.createThreadLocalRandomIntProvider(rng);
             }
             if (reverseBits) {
                 rng = RNGUtils.createReverseBitsIntProvider(rng);
             }
-            if (xorHashCode) {
-                rng = RNGUtils.createHashCodeIntProvider(rng);
+            // Manipulation of the bytes for the platform is done on the 
entire generator
+            if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) {
+                rng = RNGUtils.createReverseBytesIntProvider(rng);
             }
             // Run the test
             final Runnable r = new StressTestTask(testData.getRandomSource(), 
rng, output, command,

Reply via email to