Any good card shuffling functions out thar?

Started by
14 comments, last by Sculder 21 years, 4 months ago
While writing my own card game I had the same thought. A human shuffles the deck in subsets as mentioned. And I gave serious thought to trying to emulate that behavior. But in the end I decided that the way humans shuffle cards is born of necessity... we don''t have an hour to take each individual card and move it to another location after every hand played.

So after all was said and done, in the interest of fairness I thought it was more appropriate to let my code do the "simple" method. I swap two randomly selected cards in a loop for two seconds. Even on slower processors this results in thousands of swaps which I think results in a more truly randomized deck.

Larry Johnson - Head flunky in charge of diddly-squat.

www.sillisoft.com
Larry Johnson - Head flunky in charge of diddly-squat.www.sillisoft.com
Advertisement
I don''t know about you, but when you shuffle a deck IRL then you split the deck in two decks with equal lenght and then you simply make a new deck where you put first a card from deck 1 then one from deck 2 and so on. Why not do it that way? It is the real way, isnt it?

You know, if i hade been better at english, then this wouldn''t have sound as crappy.

/ Jolle
quote:Original post by l99057j
I swap two randomly selected cards in a loop for two seconds. Even on slower processors this results in thousands of swaps which I think results in a more truly randomized deck.


This sounds unnecessarily complicated ... why not just pick a random card (0-52) for the first position of the new deck, a random card remaining card (0-51) for the second, and so forth. (I''d probably look into Stoffel''s suggestion, unless I were really into reinventing the wheel, but I don''t know the intricacies of how the STL algorithm operates.)
Knuth discusses this in the Art of Computer Programming. His algorithm is simple and effective... do a dejanews search. (or buy the book ) Another common approach is to just assign a random number to each card in the deck and then sort the cards based on this number. However, for this method you need a good random number generator, and good seeding. This is the method that ParadisePoker uses for deck shuffling.

Something you do *NOT* really want to do is simulate physical human shuffling, if the goal is to randomize the cards! That would be pointless.
(Tangent. I remember hearing once that if you perfectly shuffle 7 times you''ll get the deck right back in the starting order.)

Stefu: remind me never to play poker with you.

Miserable:
quote:
The first template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last - first), where M is a value from some uniform random distribution over the range [0, N). Thus, the function randomly shuffles the order of elements in the sequence.

There''s also a functor overload that lets you substitute your own random number generator in place of whatever random_shuffle happens to be using internally (rand () would be a good guess).
Seems like the physical shuffling model is being made a lot more complicated than it should be, and still not that accurate. If you really want to model a human shuffle, just think about exactly what happens when a human shuffles cards:

- start with a stack of cards in whatever order
- split it into two roughly equal portions, i.e. stack 1 contains whole deck, take half (+-5/10) of that array right off the "top" and put it in another array (stack)
- make a third, empty array to represent the freshly shuffled deck
- continue to alternately take 1-5 cards from the "bottom" of each of the first two stacks and add them to the third stack until you''re out of cards in the first two stacks.

Might not be blazingly fast or the best shuffle, but it will be a pretty accurate approximation of the standard two-handed human shuffle and it''ll run plenty fast for a card game. It''s also easier to write (IMHO) than something that shuffles cards in place (one array).

This topic is closed to new replies.

Advertisement