commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [02/25] [math] MATH-1335
Date Tue, 17 May 2016 15:49:35 GMT
MATH-1335

Use new RNG API.


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

Branch: refs/heads/develop
Commit: 0caba0845d4363025e619eb3db6f3138b2c3d394
Parents: 1b5aef8
Author: Gilles <erans@apache.org>
Authored: Thu May 12 16:43:09 2016 +0200
Committer: Gilles <erans@apache.org>
Committed: Tue May 17 15:30:23 2016 +0200

----------------------------------------------------------------------
 .../math4/random/UniformRandomGenerator.java    | 36 +++++++++++---------
 .../random/UniformRandomGeneratorTest.java      |  8 ++---
 2 files changed, 22 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/0caba084/src/main/java/org/apache/commons/math4/random/UniformRandomGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/random/UniformRandomGenerator.java b/src/main/java/org/apache/commons/math4/random/UniformRandomGenerator.java
index aac8295..1a07ba4 100644
--- a/src/main/java/org/apache/commons/math4/random/UniformRandomGenerator.java
+++ b/src/main/java/org/apache/commons/math4/random/UniformRandomGenerator.java
@@ -18,39 +18,41 @@
 package org.apache.commons.math4.random;
 
 import org.apache.commons.math4.util.FastMath;
+import org.apache.commons.math4.rng.UniformRandomProvider;
 
 /**
  * This class implements a normalized uniform random generator.
- * <p>Since it is a normalized random generator, it generates values
- * from a uniform distribution with mean equal to 0 and standard
- * deviation equal to 1. Generated values fall in the range
- * [-&#x0221A;3, +&#x0221A;3].</p>
+ *
+ * <p>
+ * It generates values from a uniform distribution with mean
+ * equal to 0 and standard deviation equal to 1.
+ * Generated values fall in the range \( [-\sqrt{3}, +\sqrt{3}] \).
+ * </p>
  *
  * @since 1.2
  */
 public class UniformRandomGenerator implements NormalizedRandomGenerator {
-
     /** Square root of three. */
-    private static final double SQRT3 = FastMath.sqrt(3.0);
-
+    private static final double SQRT3 = FastMath.sqrt(3);
     /** Underlying generator. */
-    private final RandomGenerator generator;
+    private final UniformRandomProvider generator;
 
-    /** Create a new generator.
-     * @param generator underlying random generator to use
+    /**
+     * Creates a new generator.
+     *
+     * @param generator Underlying random generator.
      */
-    public UniformRandomGenerator(RandomGenerator generator) {
+    public UniformRandomGenerator(UniformRandomProvider generator) {
         this.generator = generator;
     }
 
-    /** Generate a random scalar with null mean and unit standard deviation.
-     * <p>The number generated is uniformly distributed between -&sqrt;(3)
-     * and +&sqrt;(3).</p>
-     * @return a random scalar with null mean and unit standard deviation
+    /**
+     * Generates a random scalar with zero mean and unit standard deviation.
+     *
+     * @return a random scalar in the range \( [-\sqrt{3}, +\sqrt{3}] \).
      */
     @Override
     public double nextNormalizedDouble() {
-        return SQRT3 * (2 * generator.nextDouble() - 1.0);
+        return SQRT3 * (2 * generator.nextDouble() - 1);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/0caba084/src/test/java/org/apache/commons/math4/random/UniformRandomGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/random/UniformRandomGeneratorTest.java
b/src/test/java/org/apache/commons/math4/random/UniformRandomGeneratorTest.java
index fbe9271..68b133e 100644
--- a/src/test/java/org/apache/commons/math4/random/UniformRandomGeneratorTest.java
+++ b/src/test/java/org/apache/commons/math4/random/UniformRandomGeneratorTest.java
@@ -18,8 +18,8 @@
 package org.apache.commons.math4.random;
 
 import org.apache.commons.math4.random.JDKRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
-import org.apache.commons.math4.random.UniformRandomGenerator;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.stat.StatUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -29,8 +29,7 @@ public class UniformRandomGeneratorTest {
 
     @Test
     public void testMeanAndStandardDeviation() {
-        RandomGenerator rg = new JDKRandomGenerator();
-        rg.setSeed(17399225432l);
+        final UniformRandomProvider rg = RandomSource.create(RandomSource.ISAAC, 17399225432L);
         UniformRandomGenerator generator = new UniformRandomGenerator(rg);
         double[] sample = new double[10000];
         for (int i = 0; i < sample.length; ++i) {
@@ -39,5 +38,4 @@ public class UniformRandomGeneratorTest {
         Assert.assertEquals(0.0, StatUtils.mean(sample), 0.07);
         Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02);
     }
-
 }


Mime
View raw message