P23 { public static <T> List<T> randomSelectR(List<T> list, int n) { if (n == 0) { return Collections.emptyList(); } Object[] objects = P20.removeAt0(list, new Random().nextInt(list.size())); List<T> remaining = (List<T>) objects[0]; T t = (T) objects[1]; List<T> result = new ArrayList<>(); result.add(t); result.addAll(randomSelectR(remaining, n - 1)); return result; }  static List<T> randomSelect(List<T> list, int n); static List<T> randomSelectR(List<T> list, int n); static List<T> randomSelect_imperative(List<T> list, int n);  }
@Test public void shouldReturnAListOfThreeRandomSelectedElements_recursive() throws Exception { List<String> result = P23.randomSelectR(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), 3); System.out.println(result); assertThat(result, hasSize(3)); }