Wednesday, August 16, 2006
Poker Math Geeks Unite!
Cactus Kev, poker aficionado, has written a seriously hard-core poker hand evaluator. The real secret of it is in two parts.
First, the algorithm makes a difference between unique hands--any of the 2,598,960 (C(52,5) in combinatoric parlance) possible hands in poker--and distinct hands--those hands that will tie in a game of cards even though they're different (e.g. a ten-high straight of TH-9D-8D-7D-6D ties a ten-high straight of TS-9C-8C-7C-6C), and of which turn that 2,598,960 unique hands into 7462 distinct hands.
Cactus Kev then needed to be able to evaluate a hand with the cards in an arbitrary order (sorting it would waste CPU cycles), which brings us to the second strength of the algorithm: a prime number was assigned to each value of card (e.g. Two is 2, Three is 3, Four is 5, Five is 7, and so on until Ace is 41). Now he could simply multiply the five cards together and get a unique number, which he could then compare against any of the 7462 distinct hands to deterimine which hand he had.
Paul Senzee of Florida then further optimized the algorithm to include a pre-computed perfect hash, which knew all of the distinct hand ids in advance so as to remove any unnecessary CPU cycles.
This algorithm, on an unspecified computer (probably a Pentium 4 class machine), calculates a poker hand in arbitrary order in 63 milliseconds. That's almost 16 hands per second to identify the rank of any of the 2,598,960 poker hands. Kudos, gentlemen!