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?

Edited 5 Years Ago by Narue: added code tags

Welcome to the forum, Bubbinos! ;)

Change your logic. Start with the array of numbers, being assigned to the array:

for(i=0;i<52;i++) {  //eliminate repeated numbers
  array[i] = i;
}
//now pick a random card
for(i=0;i<(NumPlayers * 5); i++) {
  j = rand() % 52 + 1; //range of 1 to 52
  hand[i] = array[j]; //a unique random number
}

I haven't run the above, but I believe it's OK. Notice how nice and easy it is to study it, when it's between tags.

You should ALWAYS use code tags - just click on the [code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] tags.

You should ALWAYS use code tags - just click on the icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.

Edited 5 Years Ago by Adak: n/a

Yes, that code is bogus < facepalm >

The first part is right

for(i=0;i<52;i++) { //eliminate repeated numbers
   array[i] = i;
}

The second part is goofed. All you should do there is pick a random two number from 0 to 51, and then swap them, using the two random numbers as the index into the array[].

for(i = 0; i < someNumber; i++) {
  //adjust for cards numbered 1 to 52, if you have that set up
  rand1 = rand() % 52;  //range is 0 to 51 
  rand2 = rand() % 52; 
  if(rand1 != rand2) {
     temp = array[rand1];
     array[rand1] = array[rand2];
     array[rand2] = temp;
  }
}

What should "someNumber" be? I'll leave that as an exercise for the student. ;)

Edited 5 Years Ago by Adak: n/a

This question has already been answered. Start a new discussion instead.