Numbering system for poker hands

191 Views Asked by At

I figure that this is surely either a solved problem or it is provably impossible. But I'm not able to track down an answer.

There are ${52 \choose 5}$ = 2,598,960 distinct ways to deal five cards from a standard 52 card deck. In poker one would often collapse many of these, since hands different only by suit are equivalent. But I'm considering all distinct hands.

Are there standard ways to number these hands?

We could think of this as a bijective function between the first 2,598,960 positive integers and all possible 5 card hands.

It would allow me to say, "Player 1 has hand #357." And then I could apply $f(357)$ to get the 5 cards.

Clearly I could define an ordering for all 2,598,960 hands. And then I could iterate through them all until I find the 357th entry. But it seems to me that there must be a far more elegant number system that would avoid the need to iterate through? Is there?

2

There are 2 best solutions below

0
On

First, identify the $52$ cards with the numbers $0,1,2,\ldots, 51$.

Next arrange your hand of $n(=5)$ cards in ascending order $a_1<a_2<\ldots<a_n$. There are $a_n\choose n$ hands of $n$ cards with highest card $<a_n$, so we will assign the number ${a_n\choose n}+\text{something}$ to this hand. To compute the "something", we may note that $(a_0,\ldots,a_{n-1})$ is in fact a hand of $n-1$ cards that can be numbered by the same method. By repeating this, we finally arrive at $${a_n\choose n}+{a_{n-1}\choose n-1}+\cdots +{a_2\choose 2}+{a_1\choose 1}. $$ Note that this assigns the number $0$ to the lowest hand (which is no surprise as we essentially counted the number of "smaller" hands).

Now for the converse: Given a number $0\le m<{52\choose 5}$, how do we find the cards $a_1,\ldots, a_n$? In principle, it is easy: Just find the maximal $a$ with ${a\choose n}\le m$. Then $a_n=a$ and we rinse and repeat with $m-{a\choose n}$ instead of $m$ and $n-1$ instead of $n$. But how to find that maximal $a$? Note that ${a\choose n}=\frac{a(a-1)\cdots(a-n+1)}{n!}$ so that $a^n>n!{a\choose n}>(1-n+1)^n$ and hence we might simply try the few values from $\lceil\sqrt[n]m\rceil$ down to $\lceil\sqrt[n]m\rceil-n+1$.

0
On

Here's one way to do it. It requires a little pre-computation, but it will be fast once you get started.

Number the cards $1$ to $52$ in some order. Consider the cards in each hand to be sorted in descending order, and then order the hands lexicographically, so that the hands are $$ 1:\ 54321\\ 2:\ 64321\\ 3:\ 65321\\ 4:\ 65421\\ 5:\ 65431\\ 6:\ 65432\\ 7:\ 74321\\ \vdots$$ Now to find hand number $357$ note that there are $$\binom{11}{4}=330$$ hands that begin with card $12$ and $$\binom{12}{4}=429$$ hands that begin with card $13$. Therefore, we can say that hand $357$ starts with card $13$ and further, that it the $26$th hand that starts with cards $13$.

Now apply the same procedure to find the $27$ four-card hand. We have $$\binom73=35,\ \binom63=20$$ so the second-highest card in the hand must be card $8$. Proceed in this manner until all cards in the hand have been determined.

If you have a lot of hands to work with as you say, it will be faster to pre-compute a table of binomial coefficients. If you want to get fancy, you can make an inverted list, and locate the appropriate value by a modified binary search.