So I decided to create a simple card deck shuffling program, nothing fancy just a single array with 52 "cards" numbered from 0 to 52. I'm not necessarily sure if it is the best method.
I created a random integer method (randInt) to get random numbers and a shuffling method (shuffle) which gets two random cards and swaps their values.
My method is simple enough but when I run the code (and some debug code I added) I seem to get each value becoming the exact same value (d[2] = d[1] = randCard1) which results in multiples of the same card and obviously other values completely disappearing. Given enough shuffles the whole deck would likely become 52 cards all consisting of the same value.
I have read some questions about swapping integer arrays but my question is different as it is about the issue of using a for loop, (as my method of swapping array values is similar to those questions already, but final/static keywords aren't helpful when I am modifying a few times I have concluded that for loops are to blame) which apparently prevents the arrays from updating properly.
Is this the case if so how do I over come this and if not what am I missing? Is there another problem with my way of swapping integer array values?
Here is my code:
import java.util.Random;
public class Cards {
public static void main(String[] args) {
int[] deck = new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51
};
printArray(deck, "Original Deck:");
deck = shuffle(deck);
printArray(deck, "Shuffled Deck:");
}
public static int[] shuffle(int[] d) {
//shuffle between 100 to 200 to ensure a thorough shuffle
int randLoops = randInt(100, 200);
int randCard1;
int randCard2;
for (int i = 0; i <= randLoops; i++) {
int temp;
randCard1 = randInt(0, 51);
do {randCard2 = randInt(0, 51);}
while (randCard2 == randCard1);
System.out.print("*" + randCard1 + "|" + randCard2 + "/");
temp = randCard1;
d[randCard1] = d[randCard2];
d[randCard2] = d[temp];
System.out.println(d[randCard1] + "|" + d[randCard2] + "*");
}
return d;
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void printArray(int[] d, String t) {
System.out.println(t);
for (int i = 0; i < d.length; i++) {
System.out.print(d[i] + ", ");
}
System.out.println();
}
}
Here is an example of my output with debugging code (hopefully it helps and doesn't hinder):
Original Deck:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
*40|41/41|41*
*29|40/41|41*
*19|26/26|26*
*2|27/27|27*
*4|30/30|30*
*24|25/25|25*
*7|1/1|1*
*17|47/47|47*
*14|5/5|5*
*42|11/11|11*
*23|42/11|11*
*33|30/30|30*
*19|42/9|9*
*11|26/9|9*
*40|21/9|9*
*36|11/9|9* <--------- at the end I don't know why the values = 9?
Shuffled Deck:
9, 11, 30, 11, 30, 30, 31, 45, 11, 11, 51, 9, 11, 45, 45, 45, 51, 11, 9, 9, 44, 9, 30, 44, 27, 11, 9, 27, 31, 11, 11, 21, 11, 11, 45, 35, 9, 11, 11, 30, 9, 11, 9, 11, 21, 31, 9, 30, 45, 30, 11, 45,
BUILD SUCCESSFUL (total time: 2 seconds)
temp = randCard1;should betemp = d[randCard1];Collections.sort(deck, new RandomComparator())where the comparators compare function is justreturn rand.nextInt(2) - 1;or something like that.