You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/01/26 14:59:28 UTC

commons-rng git commit: Usage example.

Repository: commons-rng
Updated Branches:
  refs/heads/master a242b59f7 -> da6512c75


Usage example.

Code used for "visual" check while debugging RNG-37.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/da6512c7
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/da6512c7
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/da6512c7

Branch: refs/heads/master
Commit: da6512c759f863c65b1c3e44b60d41f636b0812d
Parents: a242b59
Author: Gilles <er...@apache.org>
Authored: Fri Jan 26 15:56:03 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Fri Jan 26 15:56:03 2018 +0100

----------------------------------------------------------------------
 commons-rng-examples/pom.xml                    |  6 ++
 .../sampling/UniformSamplingVisualCheck.java    | 80 ++++++++++++++++++++
 2 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/da6512c7/commons-rng-examples/pom.xml
----------------------------------------------------------------------
diff --git a/commons-rng-examples/pom.xml b/commons-rng-examples/pom.xml
index 0bf185b..9e7292c 100644
--- a/commons-rng-examples/pom.xml
+++ b/commons-rng-examples/pom.xml
@@ -51,6 +51,12 @@
       <artifactId>commons-rng-simple</artifactId>
       <version>1.1-SNAPSHOT</version>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-rng-sampling</artifactId>
+      <version>1.1-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 
 </project>

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/da6512c7/commons-rng-examples/src/main/java/org/apache/commons/rng/examples/sampling/UniformSamplingVisualCheck.java
----------------------------------------------------------------------
diff --git a/commons-rng-examples/src/main/java/org/apache/commons/rng/examples/sampling/UniformSamplingVisualCheck.java b/commons-rng-examples/src/main/java/org/apache/commons/rng/examples/sampling/UniformSamplingVisualCheck.java
new file mode 100644
index 0000000..7e442a8
--- /dev/null
+++ b/commons-rng-examples/src/main/java/org/apache/commons/rng/examples/sampling/UniformSamplingVisualCheck.java
@@ -0,0 +1,80 @@
+/*
+ * 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.sampling;
+
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
+
+/**
+ * Creates 2D plot of sampling output.
+ * It is a "manual" check that could help ensure that no artefacts 
+ * exist in some tiny region of the expected range, due to loss of
+ * accuracy, e.g. when porting C code based on 32-bits "float" to
+ * "Commons RNG" that uses Java "double" (64-bits).
+ */
+public class UniformSamplingVisualCheck {
+    /** RNG. */
+    private final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S);
+    /** Samplers. */
+    private final ContinuousSampler[] samplers = new ContinuousSampler[] {
+        new ZigguratNormalizedGaussianSampler(rng),
+        new MarsagliaNormalizedGaussianSampler(rng),
+        new BoxMullerNormalizedGaussianSampler(rng),
+    };
+
+    /**
+     * Program entry point.
+     */
+    public static void main(String[] args) {
+        final float lo = 0.1f;
+        final int bands = 2;
+        float hi = lo;
+        for (int i = 0; i < bands; i++) {
+            hi = Math.nextUp(hi);
+        }
+        System.out.printf("# lo=%.10e hi=%.10e", lo, hi);
+        System.out.println();
+
+        final UniformSamplingVisualCheck app = new UniformSamplingVisualCheck();
+
+        long n = 0;
+        while (true) {
+            System.out.printf("%.16e\t", app.rng.nextDouble());
+
+            for (ContinuousSampler s : app.samplers) {
+                while (true) {
+                    final double r = s.sample();
+                    if (r < lo ||
+                        r > hi) {
+                        // Discard numbers outside the tiny region.
+                         continue;
+                     }
+
+                    System.out.printf("%.16e ", r);
+                    break;
+                }
+            }
+
+            System.out.println();
+        }
+    }
+}