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 2013/07/31 16:00:51 UTC

svn commit: r1508860 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java

Author: erans
Date: Wed Jul 31 14:00:50 2013
New Revision: 1508860

URL: http://svn.apache.org/r1508860
Log:
MATH-1011
Delegate method "nextInt" in "RandomDataGenerator" to method "sample" in
"UniformIntegerDistribution".

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java?rev=1508860&r1=1508859&r2=1508860&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java Wed Jul 31 14:00:50 2013
@@ -37,6 +37,7 @@ import org.apache.commons.math3.distribu
 import org.apache.commons.math3.distribution.TDistribution;
 import org.apache.commons.math3.distribution.WeibullDistribution;
 import org.apache.commons.math3.distribution.ZipfDistribution;
+import org.apache.commons.math3.distribution.UniformIntegerDistribution;
 import org.apache.commons.math3.exception.MathInternalError;
 import org.apache.commons.math3.exception.NotANumberException;
 import org.apache.commons.math3.exception.NotFiniteNumberException;
@@ -194,25 +195,7 @@ public class RandomDataGenerator impleme
 
     /** {@inheritDoc} */
     public int nextInt(final int lower, final int upper) throws NumberIsTooLargeException {
-        if (lower >= upper) {
-            throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
-                                                lower, upper, false);
-        }
-        final int max = (upper - lower) + 1;
-        if (max <= 0) {
-            // the range is too wide to fit in a positive int (larger than 2^31); as it covers
-            // more than half the integer range, we use directly a simple rejection method
-            final RandomGenerator rng = getRandomGenerator();
-            while (true) {
-                final int r = rng.nextInt();
-                if (r >= lower && r <= upper) {
-                    return r;
-                }
-            }
-        } else {
-            // we can shift the range and generate directly a positive int
-            return lower + getRandomGenerator().nextInt(max);
-        }
+        return new UniformIntegerDistribution(getRandomGenerator(), lower, upper).sample();
     }
 
     /** {@inheritDoc} */