Shuffle Function

I'm currently creating a playing card memory game in my programming class, and I'm having a lot of difficulty creating a shuffle function for the deck.

The way that I set up the program was such that I have two arrays of 52 elements. The first array, unshuffleddeck[52] was filled with numerical values 1-52 in order, and the second array, shuffleddeck[52] was left blank. The function is supposed to pick a random element from unshuffleddeck, and copy it to shuffleddeck, unless that particular element of unshuffleddeck is equal to zero, in which case it loops until a non-zero value is chosen. When a non-zero value is chosen, it should copy that value to shuffleddeck, and directly after that reset that specific element of unshuffleddeck to zero, thus removing it from as a possibility for the next element of shuffleddeck. That's what I intended to do, here's what I ended up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void shuffle(void)
{
    cout<<"Shuffling, Please wait......\n";
    for (n=1;n<53;n++)
        {
        do
            {
            temp = ((rand()%52)+1);
            if (unshuffleddeck[temp]==0) //Checks for repeats. See below
            dupe = true;
            else
                    {
                    dupe = false;
                    shuffleddeck[n]=temp; /*supposed to fill concurrent     elements of shuffleddeck with values that aren't repeated.*/                 
                    unshuffleddeck[temp]=0;/*Supposed to *remove* the selected value as a possibility for the next element of unshuffleddeck*/
                    cout<<endl<<temp<<endl<<n;
                    }
            }
        while (dupe == true);
        }
    cout<<"shuffling complete.\n";//Just here to see if the loop finished
    return ;
}  


As far as I can see, it should assign values for shuffleddeck which don't repeat, but it does not. When I run it, it will fill shuffleddeck with random values, but it doesn't prevent repeats. I have spent several classes attempting to make the function work, but I'm starting to fall behind.

I would appreciate if someone could tell me how I can change this specific function to have it work, but if it's completely illogical I will scrap it and try something else. Thanks.
A possible solution would also be to rotate the deck a random number of times and then just pop the first or last value into the new shuffled deck until the deck is empty. Though of course that would work a lot better if you had a list instead of an array.
I'll give you help rather than an answer.

initialise your array[ 52 ] = 0;

Create a loop;
Create a random number;
Loop through your array[ 52 ], checking to see if the number has already been used;

if( number in array == 0 ), save number;

if( number has been used ), loop variable--
( This is to make sure you still end up with 52 different numbers )


By doing this, each number would represent a card. So they would end up shuffled.
Last edited on
std::random_shuffle would be the easiest to do.
hmm, or that lol. Never knew that existed.
A simple shuffle algorithm is to start at the first element, randomly select another element (rand()%ARRAY_SIZE + 1), then swap the first element with the second. Then repeat for the second element, and so on until you reach the last element, which guarantees every element has been swapped at least once.

And obviously, what firedraco said

Edit: I need to proofread more.
Last edited on
Intrexa, by doing it that way, you can randomly reselect the same index twice, creating duplicates.
Lynx, that wouldn't create duplicates, it would just make that element remain at the same position - which is legal for shuffling.
Oh yeah, I read that wrong. When I read it, I was thinking of making the firstElement = secondElement, instead of swapping.
Thanks for the help. I ended up using std::random_shuffle and it worked beautifully, and I also found out that it's good practice to use if (variable==value) instead of if (variable=value). Go ahead, laugh.

Thanks again.
^Actually, the first compares for equality, the second assigns. They are quite different.
Topic archived. No new replies allowed.