### Question:
RandomUtils { public static <A> List<A> choice(Collection<A> collection, Random random, int numChoices, boolean withReplacement) { final List<Integer> indices = choiceIndex(collection.size(), random, numChoices, withReplacement); final List<A> chosen = new ArrayList<>(numChoices); for(Integer index : indices) { A element = null; if(collection instanceof List) { element = ((List<A>) collection).get(index); } else { final Iterator<A> iterator = collection.iterator(); for(int i = 0; i <= index; i++) { Assert.assertTrue(iterator.hasNext()); element = iterator.next(); } } chosen.add(element); } return chosen; } private  RandomUtils(); static void setSkipSingleElementPick(final boolean skipSingleElementPick); static boolean isSkipSingleElementPick(); static List<Integer> choiceIndex(int size, Random random, int numChoices,
                                                              boolean withReplacement); static Integer choiceIndex(int size, Random random); static List<Integer> choiceIndex(int size, Random random, int numChoices); static List<A> choice(Collection<A> collection, Random random, int numChoices, boolean withReplacement); static List<A> choice(Collection<A> collection, Random random, int numChoices); static A choice(Collection<A> collection, Random random); static List<A> choice(RandomIterator<A> iterator, int numChoices); static A choice(RandomIterator<A> iterator);  }### Answer:
@Test public void testRandomChoiceSingle() { int choice = RandomUtils.choice(list, random); Assert.assertEquals(1, choice); choice = RandomUtils.choice(list, random); Assert.assertEquals(9, choice); choice = RandomUtils.choice(list, random); Assert.assertEquals(10, choice); choice = RandomUtils.choice(list, random); Assert.assertEquals(8, choice); choice = RandomUtils.choice(list, random); Assert.assertEquals(6, choice); }
@Test public void testRandomChoiceMultiple() { final List<Integer> choice = RandomUtils.choice(list, random, 5); Assert.assertEquals(5, choice.size()); Assert.assertEquals(new Integer(1), choice.get(0)); Assert.assertEquals(new Integer(9), choice.get(1)); Assert.assertEquals(new Integer(3), choice.get(2)); Assert.assertEquals(new Integer(5), choice.get(3)); Assert.assertEquals(new Integer(10), choice.get(4)); }
@Test public void testRandomChoiceWithReplacement() { final List<Integer> choice = RandomUtils.choice(list, random, list.size() * 10, true); final Set<Integer> set = new HashSet<>(choice); Assert.assertEquals(list.size(), set.size()); }
@Test public void testRandomChoiceWithoutReplacement() { final List<Integer> choice = RandomUtils.choice(list, random, 10, false); final Set<Integer> set = new HashSet<>(choice); Assert.assertEquals(choice.size(), set.size()); }
@Test(expected = AssertionError.class) public void testRandomChoiceTooMany() { RandomUtils.choice(list, random, list.size() + 1); }