Sunday, June 11, 2017

Fisher–Yates shuffle

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle



public void shuffle() {
Random rd = new Random();
for (i = cards.size() - 1; i > 0; i--) {
int j = rd.nextInt(i + 1);
swap(i, j);
}
}
private static void swap(int i, int j) {
Card temp = cards.get(i);
cards.set(i, cards.get(i));
cards.set(j, temp);

}

No comments:

Post a Comment