Here's what I'm trying to do. I'm making a program that uses an array to store a deck of cards. I have other code written that converts a number between 1 and 52 into numbers and suits.
My only issue is that my code to randomly put numbers into the array doesn't work. I will get duplicate numbers, and hence the same card being dealt to a hand of 5 cards.
Here's the code I'm using. deck is an array of 52, which has garbage in it when passed to this function. The rand_int function just returns a random value between 1 and 52 which I store in my array at [0]. once I have two values stored in the array, it goes into the IF statement, and just compares the current random value returned (i) against all the previous values in my array.
void create_deck(int deck[])
{
int i,j;
int card_index;
for (card_index=0; card_index<52; card_index++)
{
i = rand_int(1,52);
deck[card_index] = i;
if ((card_index >0) && (card_index<52))
{
j=card_index-1;
do{
if (deck[j] == i)
{
card_index--;
break;
}
j--;
}while(j>0);
}
}
}
I opened up a watch window and paused the program after the array was filled. This is what I saw:
4 *
43
32
19
18
49
47
17
6
1
22
4 *
36
31
29
..... etc. Those are just the first 15 terms as you can see, the number 4 is in there twice.
any suggestions?