I've read many things on the topic, but I am still having misgivings about it. Please, help!
I create a deck of 52 cards:
int[] deck = new int[52];
for (int i = 0; i < deck.length; i++) {deck[i] = i;}
Now, I'd like to shuffle the deck properly, not by following any pattern or function.
I am going to use the Math.random() method in my algorithm:
for (int i = 0; i < deck.length; i++) {
int j = (int)(Math.random() * deck.length); // Get a random index out of 52
int temp = deck[i]; // Swap the cards
deck[i] = deck[j];
deck[j] = temp;
}
The problem I am having here, however, is that I sometimes might get duplicates; as a result, some cards/values are missing altogether.
I am almost certain that I am not discarding the indices/values I have already used, which is probably the core of the abovementioned problem. But how do I go about it?
Any suggestions? Possibly, following my same train of thought. Thanks a bunch!