RandomDataGenerator implements RandomData, Serializable { public int[] nextPermutation(int n, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException { if (k > n) { throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N, k, n, true); } if (k <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE, k); } int[] index = MathArrays.natural(n); MathArrays.shuffle(index, getRandomGenerator()); return MathArrays.copyOf(index, k); } RandomDataGenerator();  RandomDataGenerator(RandomGenerator rand); String nextHexString(int len); int nextInt(final int lower, final int upper); long nextLong(final long lower, final long upper); String nextSecureHexString(int len); int nextSecureInt(final int lower, final int upper); long nextSecureLong(final long lower, final long upper); long nextPoisson(double mean); double nextGaussian(double mu, double sigma); double nextExponential(double mean); double nextGamma(double shape, double scale); int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize); int nextPascal(int r, double p); double nextT(double df); double nextWeibull(double shape, double scale); int nextZipf(int numberOfElements, double exponent); double nextBeta(double alpha, double beta); int nextBinomial(int numberOfTrials, double probabilityOfSuccess); double nextCauchy(double median, double scale); double nextChiSquare(double df); double nextF(double numeratorDf, double denominatorDf); double nextUniform(double lower, double upper); double nextUniform(double lower, double upper, boolean lowerInclusive); int[] nextPermutation(int n, int k); Object[] nextSample(Collection<?> c, int k); void reSeed(long seed); void reSeedSecure(); void reSeedSecure(long seed); void reSeed(); void setSecureAlgorithm(String algorithm, String provider); RandomGenerator getRandomGenerator();  }
@Test public void testNextPermutation() { int[][] p = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } }; long[] observed = { 0, 0, 0, 0, 0, 0 }; double[] expected = { 100, 100, 100, 100, 100, 100 }; for (int i = 0; i < 600; i++) { int[] perm = randomData.nextPermutation(3, 3); observed[findPerm(p, perm)]++; } String[] labels = {"{0, 1, 2}", "{ 0, 2, 1 }", "{ 1, 0, 2 }", "{ 1, 2, 0 }", "{ 2, 0, 1 }", "{ 2, 1, 0 }"}; TestUtils.assertChiSquareAccept(labels, expected, observed, 0.001); int[] perm = randomData.nextPermutation(1, 1); if ((perm.length != 1) || (perm[0] != 0)) { Assert.fail("bad permutation for n = 1, sample k = 1"); try { perm = randomData.nextPermutation(2, 3); Assert.fail("permutation k > n, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } try { perm = randomData.nextPermutation(0, 0); Assert.fail("permutation k = n = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } try { perm = randomData.nextPermutation(-1, -3); Assert.fail("permutation k < n < 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } } }