jeudi 31 décembre 2015

what is the purpose of variable i minus 1 within the swap method? Is - 1 really necessary for a fair randomization?

I saw this code in Oracle documentation.

The documentation states: "The shuffle method below,unlike most naive attempts at shuffling, it's fair (all permutations occur with equal likelihood, assuming an unbiased source of randomness)".

My question is what is the purpose of variable i minus 1 within the swap method? Is - 1 really necessary for a fair randomization?

public static void shuffle(List<?> list, Random rnd) {
    for (int i = list.size(); i > 1; i--)
        swap(list, i - 1, rnd.nextInt(i));
}




Aucun commentaire:

Enregistrer un commentaire