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 2016/05/17 13:28:23 UTC

[5/7] [math] MATH-1341

MATH-1341

Use "RandomUtils" instead of "RandomDataGenerator".


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

Branch: refs/heads/develop
Commit: 748cb609306ee5a046a952a0ca13974ef0b6e770
Parents: 653b468
Author: Gilles <er...@apache.org>
Authored: Mon May 16 00:59:13 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Mon May 16 00:59:13 2016 +0200

----------------------------------------------------------------------
 .../math4/stat/ranking/NaturalRanking.java      | 29 ++++++++++++--------
 .../math4/stat/ranking/NaturalRankingTest.java  |  9 +++---
 2 files changed, 21 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/748cb609/src/main/java/org/apache/commons/math4/stat/ranking/NaturalRanking.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/stat/ranking/NaturalRanking.java b/src/main/java/org/apache/commons/math4/stat/ranking/NaturalRanking.java
index d0c2ac2..404ac00 100644
--- a/src/main/java/org/apache/commons/math4/stat/ranking/NaturalRanking.java
+++ b/src/main/java/org/apache/commons/math4/stat/ranking/NaturalRanking.java
@@ -24,9 +24,9 @@ import java.util.List;
 
 import org.apache.commons.math4.exception.MathInternalError;
 import org.apache.commons.math4.exception.NotANumberException;
-import org.apache.commons.math4.random.RandomDataGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
-import org.apache.commons.math4.random.Well19937c;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
+import org.apache.commons.math4.random.RandomUtils;
 import org.apache.commons.math4.util.FastMath;
 
 
@@ -83,7 +83,7 @@ public class NaturalRanking implements RankingAlgorithm {
     private final TiesStrategy tiesStrategy;
 
     /** Source of random data - used only when ties strategy is RANDOM */
-    private final RandomDataGenerator randomData;
+    private final RandomUtils.DataGenerator randomData;
 
     /**
      * Create a NaturalRanking with default strategies for handling ties and NaNs.
@@ -98,7 +98,9 @@ public class NaturalRanking implements RankingAlgorithm {
      * @param tiesStrategy the TiesStrategy to use
      */
     public NaturalRanking(TiesStrategy tiesStrategy) {
-        this(DEFAULT_NAN_STRATEGY, tiesStrategy, new Well19937c());
+        this(DEFAULT_NAN_STRATEGY,
+             tiesStrategy,
+             RandomSource.create(RandomSource.WELL_19937_C));
     }
 
     /**
@@ -116,8 +118,11 @@ public class NaturalRanking implements RankingAlgorithm {
      * @param nanStrategy NaNStrategy to use
      * @param tiesStrategy TiesStrategy to use
      */
-    public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
-        this(nanStrategy, tiesStrategy, new Well19937c());
+    public NaturalRanking(NaNStrategy nanStrategy,
+                          TiesStrategy tiesStrategy) {
+        this(nanStrategy,
+             tiesStrategy,
+             RandomSource.create(RandomSource.WELL_19937_C));
     }
 
     /**
@@ -126,7 +131,7 @@ public class NaturalRanking implements RankingAlgorithm {
      *
      * @param randomGenerator source of random data
      */
-    public NaturalRanking(RandomGenerator randomGenerator) {
+    public NaturalRanking(UniformRandomProvider randomGenerator) {
         this(DEFAULT_NAN_STRATEGY, TiesStrategy.RANDOM, randomGenerator);
     }
 
@@ -138,21 +143,21 @@ public class NaturalRanking implements RankingAlgorithm {
      * @param randomGenerator source of random data
      */
     public NaturalRanking(NaNStrategy nanStrategy,
-                          RandomGenerator randomGenerator) {
+                          UniformRandomProvider randomGenerator) {
         this(nanStrategy, TiesStrategy.RANDOM, randomGenerator);
     }
 
     /**
      * @param nanStrategy NaN strategy.
-     * @param randomGenerator RNG.
      * @param tiesStrategy Tie strategy.
+     * @param randomGenerator RNG.
      */
     private NaturalRanking(NaNStrategy nanStrategy,
                            TiesStrategy tiesStrategy,
-                           RandomGenerator randomGenerator) {
+                           UniformRandomProvider randomGenerator) {
         this.nanStrategy = nanStrategy;
         this.tiesStrategy = tiesStrategy;
-        randomData = new RandomDataGenerator(randomGenerator);
+        randomData = RandomUtils.createDataGenerator(randomGenerator);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-math/blob/748cb609/src/test/java/org/apache/commons/math4/stat/ranking/NaturalRankingTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/stat/ranking/NaturalRankingTest.java b/src/test/java/org/apache/commons/math4/stat/ranking/NaturalRankingTest.java
index 73e0370..46b2ede 100644
--- a/src/test/java/org/apache/commons/math4/stat/ranking/NaturalRankingTest.java
+++ b/src/test/java/org/apache/commons/math4/stat/ranking/NaturalRankingTest.java
@@ -19,8 +19,8 @@ package org.apache.commons.math4.stat.ranking;
 import org.junit.Assert;
 import org.apache.commons.math4.TestUtils;
 import org.apache.commons.math4.exception.NotANumberException;
-import org.apache.commons.math4.random.JDKRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.stat.ranking.NaNStrategy;
 import org.apache.commons.math4.stat.ranking.NaturalRanking;
 import org.apache.commons.math4.stat.ranking.TiesStrategy;
@@ -172,10 +172,9 @@ public class NaturalRankingTest {
 
     @Test
     public void testNaNsFixedTiesRandom() {
-        RandomGenerator randomGenerator = new JDKRandomGenerator();
-        randomGenerator.setSeed(1000);
+        UniformRandomProvider randomGenerator = RandomSource.create(RandomSource.JDK, 1000L);
         NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
-                randomGenerator);
+                                                    randomGenerator);
         double[] ranks = ranking.rank(exampleData);
         double[] correctRanks = { 5, 3, 6, 7, 3, 8, Double.NaN, 1, 2 };
         TestUtils.assertEquals(correctRanks, ranks, 0d);