RandomDataGenerator implements RandomData, Serializable { public Object[] nextSample(Collection<?> c, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException { int len = c.size(); if (k > len) { throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE, k, len, true); } if (k <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k); } Object[] objects = c.toArray(); int[] index = nextPermutation(len, k); Object[] result = new Object[k]; for (int i = 0; i < k; i++) { result[i] = objects[index[i]]; } return result; } 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 testNextSample() { Object[][] c = { { "0", "1" }, { "0", "2" }, { "0", "3" }, { "0", "4" }, { "1", "2" }, { "1", "3" }, { "1", "4" }, { "2", "3" }, { "2", "4" }, { "3", "4" } }; long[] observed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] expected = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; HashSet<Object> cPop = new HashSet<Object>(); for (int i = 0; i < 5; i++) { cPop.add(Integer.toString(i)); } Object[] sets = new Object[10]; for (int i = 0; i < 10; i++) { HashSet<Object> hs = new HashSet<Object>(); hs.add(c[i][0]); hs.add(c[i][1]); sets[i] = hs; } for (int i = 0; i < 1000; i++) { Object[] cSamp = randomData.nextSample(cPop, 2); observed[findSample(sets, cSamp)]++; } Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 27.88); HashSet<Object> hs = new HashSet<Object>(); hs.add("one"); Object[] one = randomData.nextSample(hs, 1); String oneString = (String) one[0]; if ((one.length != 1) || !oneString.equals("one")) { Assert.fail("bad sample for set size = 1, sample size = 1"); } try { one = randomData.nextSample(hs, 2); Assert.fail("sample size > set size, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } try { hs = new HashSet<Object>(); one = randomData.nextSample(hs, 0); Assert.fail("n = k = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }