I'm creating a simple method to simulate shuffling a deck. My idea is to store the size of the original deck, and repeat the loop as long as the size is not negative.
During the loop I copy an Object (card) from the list, and place it in another list. Remove from the original list, and resume the loop.
while(size >= 0){
int random = (int) ((Math.random() * size) + 0);
shuffledDeck.add(this.orderedDeck.get(random));
orderedDeck.remove(random);
size--;
}
The size of this particular deck is
int size = this.getDeckSize()-1; //51 (52 cards, from 0 to 51)
My issue is that on several different attempts, the last card in the shuffled deck is consistently the same card as the last in the unshuffled deck. This suggests that random
is never equal to size-1
.
How can I make it so the last card is actually able to be shuffled?
(In other words, why is random
ever equal to size-1
?)