So say I have
List<String> teamList = new LinkedList<String>()
teamList.add("team1");
teamList.add("team2");
teamList.add("team3");
teamList.add("team4");
teamList.add("team5");
teamList.add("team6");
Is there a simple way of picking... say 3 out the 6 elements in this list in a randomized way without picking the same element twice (or more times)?
You can also use reservoir sampling.
It has the advantage that you do not need to know the size of the source list in advance (e.g. if you are given an
Iterable
instead of aList
.) Also it is efficient even when the source list is not random-access, like theLinkedList
in your example.Try this:
I'm assuming that there are no repeated elements in the input list, also I take the precaution of shuffling a copy for leaving the original list undisturbed. It's called like this: