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/25 19:06:56 UTC

[08/21] [math] Use code moved to "Commons RNG" (v1.1 snapshot).

Use code moved to "Commons RNG" (v1.1 snapshot).


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

Branch: refs/heads/master
Commit: c3ff46e30397bc0355584c593d500d2564c1c515
Parents: c4218b8
Author: Gilles <er...@apache.org>
Authored: Thu Jan 25 17:24:55 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Thu Jan 25 17:24:55 2018 +0100

----------------------------------------------------------------------
 .../distribution/EnumeratedDistribution.java    | 25 ++++----------------
 1 file changed, 5 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/c3ff46e3/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java b/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java
index 2ca612e..149f403 100644
--- a/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java
+++ b/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java
@@ -30,6 +30,7 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.exception.NullArgumentException;
 import org.apache.commons.math4.exception.util.LocalizedFormats;
 import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.sampling.DiscreteProbabilityCollectionSampler;
 import org.apache.commons.math4.util.MathArrays;
 import org.apache.commons.math4.util.Pair;
 
@@ -172,14 +173,14 @@ public class EnumeratedDistribution<T> implements Serializable {
      * Sampler functionality.
      */
     public class Sampler {
-        /** RNG. */
-        private final UniformRandomProvider random;
+        /** Underlying sampler. */
+        private final DiscreteProbabilityCollectionSampler<T> sampler;
 
         /**
          * @param rng Random number generator.
          */
         Sampler(UniformRandomProvider rng) {
-            random = rng;
+            sampler = new DiscreteProbabilityCollectionSampler<T>(rng, singletons, probabilities);
         }
 
         /**
@@ -188,23 +189,7 @@ public class EnumeratedDistribution<T> implements Serializable {
          * @return a random value.
          */
         public T sample() {
-            final double randomValue = random.nextDouble();
-
-            int index = Arrays.binarySearch(cumulativeProbabilities, randomValue);
-            if (index < 0) {
-                index = -index - 1;
-            }
-
-            if (index >= 0 &&
-                index < probabilities.length &&
-                randomValue < cumulativeProbabilities[index]) {
-                return singletons.get(index);
-            }
-
-            // This should never happen, but it ensures we will return a correct
-            // object in case there is some floating point inequality problem
-            // wrt the cumulative probabilities.
-            return singletons.get(singletons.size() - 1);
+            return sampler.sample();
         }
 
         /**