#### Archived

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

# Card Games

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

## Recommended Posts

Has anybody here made a card game: If so, what is a good way to shuffle your deck?

##### Share on other sites
The best way is to simply run through the deck once, swapping every card with any card that hasn''t been swapped yet (ie select from any card whose index is greater than or equal to the one you''re filling in.) That gives you a perfectly smooth distribution of all the possible shuffles.

-Brian

##### Share on other sites
I know I''m starting to sound like a one-trick pony, but...
STL has a random_shuffle function in <algorithm>.

If you''re using an STL container, just do random_shuffle (cont.begin (), cont.end ())

If you''re using an array, do random_shuffle (array, array + size_of_array).

example:
#include <algorithm>using namespace std;void main (){  int cards[52];  for (int i=0; i<52; i++)    cards[ i ] = i;  random_shuffle (cards, cards + 52); // ta da}

The actual shuffle algorithm used is to proceed through the array once and swap each element with a randomly-indexed element of the array.

##### Share on other sites
To clarify, the algorithm is basically the one I described. But, yes, I forgot that algorithm was one of the ones included. I was thinking it wasn''t (due to a misinformed discussion elsewhere.) Anyways, that''s your best bet. (Thanks, Stoffel)

-Brian

##### Share on other sites
If you want, you might consider instead of doing a random shuffle, do a "perfect" shuffle. That is take the top half the cards and map them in order to all the even slots and then take the bottom half the the cards and map them in order to all the odd slots. Then give the player shuffling options like: "seven perfect shuffles with cut". You can have the dealer cheat by things like not alternating bottoms, etc.

##### Share on other sites
Isn''t it true that if you perfectly shuffle some N number of times, all the cards will be back in the same order? I think I recall that from one of the gambling movies I saw.

"There are two kinds of people in this world: people with a loaded gun, and people who dig. You dig."

##### Share on other sites
Not necessarily true if you alternate bottoms randomly.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011399
• ### Who's Online (See full list)

There are no registered users currently online

×