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?

  • $\begingroup$ In practice, of course, this is done, if a bit informally. One speaks, say, of a "Jack high straight" and such. Still, you pretty well have to record, say, each flush...as (in principle) these would be ranked. At least you could divide the number of flushes by $4$, and the number of straights by the ways to assign suits. Shouldn't be hard to go through each hand type to come up with a count. $\endgroup$
    – lulu
    Jun 7, 2020 at 15:07
  • 1
    $\begingroup$ Reminds me of this Unique representation ID for 5-card poker hand using combination without sorting $\endgroup$
    – Sil
    Jun 7, 2020 at 15:12
  • 2
    $\begingroup$ @Sil It would be easier to just use a bit vector and convert it to an integer, though. $\endgroup$
    – saulspatz
    Jun 7, 2020 at 15:15
  • $\begingroup$ As an aside, it is not unheard of for various card games to apply a distinction in value based on suit as well. For example, Contract Bridge explicitly treats $\clubsuit < \diamondsuit < \heartsuit < \spadesuit$ so it is not true that hands differing only by suit are considered the same. In the incredibly rare circumstance that the cards happened to be dealt such that each player received all of the cards of their respective suit it would explicitly be the player with all the spades who would successfully bid and make the grand slam. $\endgroup$
    – JMoravitz
    Jun 7, 2020 at 15:28
  • 1
    $\begingroup$ Thanks to both answerers! Both answers seem reasonably clear to me. I'll take some time to digest them more fully. But providing this framework for thinking about the problem is really helpful and really appreciated. $\endgroup$
    – mdahlman
    Jun 8, 2020 at 4:38

2 Answers 2


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$.


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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .