Card shuffling question.

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Originally posted by: Kyteland
it is 8 or 52 depending on how you do it. Assuming all cards are numbered 1-52 then shuffling them as such:

1-2-3-4-5-6... -> 1-27-2-28-3-29...
goes back in order after 8 shuffles

1-2-3-4-5-6... -> 27-1-28-2-29-3...
goes back in order after 52 shuffles

Edit: I was close with 7. I've figured this out before and knew it was somewhere near that.

^
*Sigh*

I was going to post my code at the time, but all the c++ arrays using the variable "i" got messed up by fusetalk.

#include <fstream.h>

int oldDeck[52] = {0};
int newDeck[52] = {0};

void sortDeck()
{
int i;

for ( i=0; i<52; i++)
{
oldDeck[i ] = i;
newDeck[i ] = 0;
}
}

void moveDeck()
{
int i;

for (i=0; i<52; i++)
{
oldDeck[i ] = newDeck[i ];
}
}

bool checkDeckOrder()
{
int i;
bool retVal = false;

for (i=0; i<52; i++)
{
if (oldDeck[i ] != i)
{
retVal = true;
}
}

return retVal;
}

void shuffle1()
{
int i;

for (i=0; i<26; i++)
{
newDeck[2*i] = oldDeck[i ];
newDeck[2*i+1] = oldDeck[i+26];
}

moveDeck();
}

void shuffle2()
{
int i;

for (i=0; i<26; i++)
{
newDeck[2*i] = oldDeck[i+26];
newDeck[2*i+1] = oldDeck[i ];
}

moveDeck();
}

void main()
{
bool stopFlag = true;
int count = 0;

sortDeck();

while (stopFlag)
{
count++;
shuffle1();
//shuffle2();
stopFlag = checkDeckOrder();
}

cerr << count << endl;
}