05-24-2005, 07:13 AM
here's my take on it - it's not the fastest but it most closely simulates the act of shuffling a deck as it might be done by a professional type
Code:
int card[52], temp[52], i, shuffles, cutsplit, lefthand, righthand, whichhand;
for (i=0; i < 52; i++)
card[i] = i; // reset the deck
for (shuffles=1; shuffles <= 7; shuffles++) // shuffle the deck 7 times
{
for (i=0; i < 52; i++)
temp[i] = card[i];
cutsplit = rand() % 3 + 24; // cut the deck, make it slightly uneven sometimes
lefthand = 0;
righthand = cutsplit;
whichhand = 1;
for (i=0; i < 52; i++)
{
if (rand() % 3 == 0) whichhand = -whichhand; // the act of shuffling will typically alternate left to right but add a 33% randomizing factor.
if (whichhand == 1 && lefthand >= cutsplit) whichhand = -whichhand; // if theres no card in the designated half, switch halves
else if (whichhand == -1 && righthand >= 52) whichhand = -whichhand;
if (whichhand == 1) deck[i] = temp[lefthand++ ];
else if (whichhand == -1) deck[i] = temp[righthand++];
whichhand = -whichhand;
}
}
ignatures suck