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). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1512306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c8fc11146f
commit
6d0394d27d
|
@ -46,6 +46,7 @@ import org.apache.commons.math3.exception.NotStrictlyPositiveException;
|
|||
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 implements RandomData, Serializable {
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue