Recently I picked up the game of Gin Rummy, which is a card game for two players using a standard deck of 52 cards (Gin_Rummy). Players always have hands of 10 cards, and the strongest kind of hand in the game is called a Gin, where all cards can be used to create melds. I was curious, what is the probability of a player being dealt Gin right away?
A single deal of the cards. That is, from time the cards are dealt until they are dealt again. Also called a hand (in poker anyway). It also refers to a set of cards assembled for scoring in a round of a card game, as in 2 sets of 3 of a kind in trick 1.
Gin has two kinds of melds: Sets of 3 or 4 cards sharing the same rank, e.g.8♥8♦8♠; and runs of 3 or more cards in sequence, of the same suit. e.g. 3♥4♥5♥ or more. In order to make Gin (melds with all 10 cards), it’s clear that you either need two runs of 5 cards, or a combination of melds of sizes 4-3-3. Here are some examples:
(5-5 split)
A♥ 2♥ 3♥ 4♥ 5♥ 9♠ 10♠ J♠ Q♠ K♠
(4-3-3 split)
4♥ 4♠ 4♦ 4♣ 6♥ 7♥ 8♥ 2♣ 2♥ 2♠
In order to find the probability we simply need to count the number of possible Gin hands, and divide by the number of hands of size 10, given mathematically by 52 Choose 10. While it’s probably possible to count the Gin hands “by hand” with combinatorics, that task was too annoying for me to do at the time, and so I chose to devise an algorithm to count them instead.
When I started thinking about this problem, I found a web forum where one user had devised a brute force algorithm, iterating over all possible hands, to count the number of Gin hands, which took several hours to compute. Rather than iterate through every possible hand of 10 cards, my approach was to first compute all the possible melds that can be made, and then combine those melds (in the combinations 5-5 and 4-3-3), to see if they were valid hands. A combination of melds could be invalid if the melds have the same cards, for example, the following three melds all contain a 4:
4♥ 4♠ 4♦ 4♣ 3♥ 4♥ 5♥ 4♣ 5♣ 6♣
In the case above, the hand is not a valid hand of 10 cards, so this combination of melds is not counted as a Gin hand.
The algorithm also had to be careful not to count hands twice. This is possible because some hands of 10 cards can actually make Gin in multiple ways. For example, the following meld combinations are actually made from the same hand:
A♣2♣3♣4♣5♣ 6♣7♣8♣9♣10♣
A♣2♣3♣4♣ 5♣6♣7♣ 8♣9♣10♣
A♣2♣3♣ 4♣5♣6♣7♣ 8♣9♣10♣
A♣2♣3♣ 4♣5♣6♣ 7♣8♣9♣10♣
For this reason, when building hands from melds, the algorithm needs to check if the resulting hand has been made before. I achieve this in my algorithm by inserting Gin hands into a hash table the first time they are made, and check this table for every meld combination, so that hands are not double counted.
I wrote the algorithm in C++, and the resulting code runs in about 500 milliseconds on my machine. I have made the source code available.
The result is that there are 51200 possible hands that make Gin. This means the probability of being dealt Gin is 1 in 308,984