I saw this code in the Oracle documentation.
The documentation states: "The shuffling method is given below, unlike the most naive shuffling attempts, it is fair (all permutations occur with equal probability, assuming an unbiased source of randomness)."
My question is, what is the purpose of the variable i minus 1 in the swap method? Is 1 really necessary for honest randomization?
public static void shuffle(List<?> list, Random rnd) {
for (int i = list.size(); i > 1; i--)
swap(list, i - 1, rnd.nextInt(i));
}
source
share