#### Archived

This topic is now archived and is closed to further replies.

# Any good card shuffling functions out thar?

This topic is 5811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, I was wondering what other people write for a card shuffling function. I just use a simple array, which I then randomize, and it doesn’t quite seem to mimic what happens to a real-life deck when shuffled. Does anyone have a really good (sophisticated) card shuffling function? I don’t care what code it’s in. I’ll convert it to whatever code I decide to use. Thanks ___________________________ "It’s been a very long time since I’ve ceased to be preoccupied with reality." -Alfred Hitchcock

##### Share on other sites
Why not just have an array of type card with variables suit, value and taken in order?

Then use random to pick a number from 0-51, see if the card is taken, if it''s not set it to taken or if it is, proceed through the array until you find one that isn''t. If there aren''t any cards available, reset them all to taken=false and pick another random number.

You could use that same routine to take a sorted deck and randomize it into a second array of type card and go back and forth a few times. Then use the routine to pick a card from the randomized deck. Or go incrementally.

Ben

IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ]

##### Share on other sites
DIM CARDS(52)

RANDOMIZE TIMER

'Initialize the cards
FOR I=1 TO 52
CARDS(I)=I
NEXT I

' Shuffle the cards
FOR I = 1 TO 51
SWAP CARDS(I),CARDS(INT(RND*(52-I))+I+1)
NEXT I

That should work. The trick is calculating the random card to swap with.

[edited by - CodeJunkie on November 18, 2002 12:49:42 PM]

##### Share on other sites
I''ve never written a card shuffler before, so this might be a load of hot-air but:

When you shuffle a deck (depending on the shuffle you use), you''re not moving a card randomly from point a to point b. Instead, your moving a ''set'' of cards to some other position in the stack in some ''physical'' way.

When you shuffle, you''re always moving the cards in the same direction. That is- you dont move card 10 to position 1 and card 12 to position 33-- it would be more like card 10 to 1, and card 12 to 5..

Sometimes you move sets of sets. that is, 4 sets of 3 cards, from different parts of the deck.

You could try a shuffle like this:

///////

cardsInASet = rand() % 8; // move max of five cards at a time
numberOfSets = rand() % 10;
spaceBetweenSets = rand() % 10;
distanceSetMoved = rand() % 10;

startSet = rand() % 52;

for ( i = 0; i < numberOfSets; i++)
{
char cardsInThisSet;
char setOffset;
char distanceOffset;

// So every set does have exactly the same number //
cardsInThisSet = cardsInASet + ( 2 - (rand() % 4));
setOffset = 2 - rand() % 4;
distanceOffset = 2 - rand() % 4;

for ( j = 0; j < cardsInThisSet; j++)
{
char sourcePlace, destPlace;

sourcePlace = startSet + j + setOffset;
destPlace = startSet + j + setOffset + distanceMoved + distanceOffset;

destPlace = destPlace % 52;

MoveCard ( sourcePlace, destPlace);
}

startSet += spaceBetweenSets + ( 2 - (rand() % 4);
}

///////

This would simulate a well-known shuffle.

Will

##### Share on other sites
hey CodeJunkie, that''s how i do mine too. works great.

i also use a seeding function to flip the time (can''t remember why, must have picked it up from one of the PhDs i used to work with):

  unsigned GenerateSeed(){    int d, s;    unsigned Seed = (unsigned)time(NULL);    char Temp, *SeedStr = _fcvt((double)Seed, 7, &d, &s);    int i = 0, l = strlen(SeedStr);//    while ( i < l/2 )    {        Temp = SeedStr[i];        SeedStr[i] = SeedStr[l-i-1];        SeedStr[l-i-1] = Temp;        i++;    }//    srand48((long)strtod(SeedStr, NULL));//    return lrand48();}

##### Share on other sites
thanks for the replies.

This is kinda what I'm doing now. It just seemed too simple, like it should be more complex. I guess I should explain my problem I'm having in more detail. When I step through my code it seems to place them in the same random order everytime. I'm using Visual Basic (but I don't have to, I just wanted to test it out quickly). Someone once told me that the random number generator in VB isn't that great, so I was wondering if there was a better way around this.

Thanks again.

Wow! I should have waited a bit before posting. Thanks everyone, I'll give these all a try.

___________________________
"It’s been a very long time since I’ve ceased to be preoccupied with reality."

-Alfred Hitchcock

[edited by - Sculder on November 18, 2002 1:21:03 PM]

##### Share on other sites
quote:
When I step through my code it seems to place them in the same random order everytime. I''m using Visual Basic

Well if that''s all you meant, just put the following line in there before you shuffle:

RANDOMIZE TIMER

Although I love the replies about modeling physical card shuffling That''s what I thought he meant too.

##### Share on other sites
That IS what I meant. Whta ouy wnat me 2b claer wit wha i right?

actually, it was the two fold problem. Yes the Randomize Timer was missing from my code (much thanks!) but the physical problem exisited too. That makes a lot of sense how cards shuffled are not randomized but groups of cards are. I tired it out (in C++ hehehe) and it works like I wanted it. The deck before always seemed too random. Thanks again for all the replies!

##### Share on other sites
Cards deck[52];
Cards::initializeDeck (deck);
std::random_shuffle(deck, deck + 52);

##### Share on other sites
I do the one hand riffle shuffle.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632933
• Total Posts
3009300
• ### Who's Online (See full list)

There are no registered users currently online

×