commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1512306 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math3: random/RandomDataGenerator.java util/MathArrays.java
Date Fri, 09 Aug 2013 13:55:49 GMT
Author: erans
Date: Fri Aug  9 13:55:49 2013
New Revision: 1512306

URL: http://svn.apache.org/r1512306
Log:
MATH-1020.
Fixed "nextPermutation" in "RandomDataGenerator". Bug showed up when using
a fixed version of "shuffle" (MATH-1019) added in "MathArrays" (MATH-1010).
Added overloaded "shuffle" method (used in the above fix).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.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=1512306&r1=1512305&r2=1512306&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
Fri Aug  9 13:55:49 2013
@@ -46,6 +46,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.NumberIsTooLargeException;
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.util.MathArrays;
 
 /**
  * Implements the {@link RandomData} interface using a {@link RandomGenerator}
@@ -639,13 +640,10 @@ public class RandomDataGenerator impleme
         }
 
         int[] index = getNatural(n);
-        shuffle(index, n - k);
-        int[] result = new int[k];
-        for (int i = 0; i < k; i++) {
-            result[i] = index[n - i - 1];
-        }
+        MathArrays.shuffle(index, getRandomGenerator());
 
-        return result;
+        // Return a new array containing the first "k" entries of "index".
+        return MathArrays.copyOf(index, k);
     }
 
     /**

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java?rev=1512306&r1=1512305&r2=1512306&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
Fri Aug  9 13:55:49 2013
@@ -1505,4 +1505,24 @@ public class MathArrays {
             throw new MathInternalError(); // Should never happen.
         }
     }
+
+    /**
+     * Shuffle the entries of the given array.
+     *
+     * @param list Array whose entries will be shuffled (in-place).
+     * @param rng Random number generator.
+     */
+    public static void shuffle(int[] list,
+                               RandomGenerator rng) {
+        shuffle(list, 0, Position.TAIL, rng);
+    }
+
+    /**
+     * Shuffle the entries of the given array.
+     *
+     * @param list Array whose entries will be shuffled (in-place).
+     */
+    public static void shuffle(int[] list) {
+        shuffle(list, new Well19937c());
+    }
 }



Mime
View raw message