Math challenge... aka, do my work for me

scrottie on 2009-02-17T04:54:57

The Wikipedia Poker page has numbers on the various standard poker hands, but I've been tasked with scoring odd ball hands and hands with wild cards, and furthermore, with validating my scoring logic. I have test runs, representing all 52-choose-5 [footnote 1] possible hands run through the hand recognition logic (wild cards are 2s).

I was able to predict how many hands would score as most things but the wild card straight flush has me stumped. Here's what I think the number should be and why. Tell me why I'm wrong.

I'm doing two numbers and adding them together. Hands with a logical high card (which might be a wildcard) of A K Q J 10 9 8 or 7 don't use a 2 as a 2 so they have potentially all four wild cards available (but in reality, four wilds is another hand that takes priority, so we have to subtract that out later). That's 8 possible logical high card values, times 4 suits. Then there's the exact mixture of actual non-wild cards and wild cards. The five cards composing that whatever-high straight flush can be any mixture of the non-wild normal values and the four wild cards. That's 9 cards those 5 cards are chosen from, or 9-choose-5. So 8 * 4 * 9-choose-5 which is 4032.

Then the same thing over again, but for 6 and 5 high hands, where one of the wild cards has to stand in for the actual 2: 2 high card values, 4 suits for whichever of the cards aren't wild, then a mixture of five non-wild cards and the three remaining wilds, or 8-choose-5, which is 448.

Add those for 4480. Subtract out 48+4+480 for the hands four 2s, royal flush, and wild royal flush. And it's still way off what the actual Perl scoring logic came up with: 2,068.

So, what did I do wrong, and how do I fix it?

-scott

Footnote 1:

See http://en.wikipedia.org/wiki/Binomial_coefficient for the 52-choose-5 thing. It's normally written as one number over another both surrounded by (). It's the nCr button on your calculator. For example, 52-choose-5 is 2,598,960.


Duplicates...

scrottie on 2009-02-17T05:08:37

Alright, I see a problem. Duplicates. Eg,

K 2 2 2 2 and 2 K 2 2 2 are both the same hand, but the above considers one king-high and the other ace-high. (2s are wild.)

Not sure how to represent the problem now.

-scott

Re:Duplicates...

gizmo_mathboy on 2009-02-17T15:53:07

When wild cards are used in poker doesn't five of a kind become the highest hand? At least that's what I vaguely recall from Hoyle.

A brief skimming of wikipedia and the wikibooks for poker doesn't show that but then again I would think the odds of 5 of a kind shouldn't be much different than four of a kind except that it's a specific 4 cards that are needed.

Not sure how it really affects your calculations though.

Re:Duplicates...

scrottie on 2009-02-17T17:30:23

Well, this isn't real poker, but instead video poker, where the rules are made up and people like it that way. But five of a kind is the highest paying hand in one of these. The other, five 3-5's, five 6-k, and five aces are all separate hands, and flush comes before four 2s (four wilds). So the number of card combinations that have a chance to match as a hand does vary by game. Simply subtracting out things that overlap, like subtracting out non-royal flushes and four 2s seems to work, at least for the other, non-wild games.

The other night, I wrote a short program to compute all possible wild straight flushes and filter for duplicates, and it matched the scoring logic. It matched the test run. I'm not sure if I still need the math (depends on the Nevada Gaming Commission) but at this point, I'd like of like to see the solutions.

use strict;
use warnings;
 
use Algorithm::Permute;
 
# @deck = ( map($_."h", 1..13), map($_."d", 1..13), map($_."c", 1..13), map($_."s", 1..13), );
 
my @deck = ( reverse map($_."h", 1..13, 1), ); # one suite for now, then x4
 
# push @deck, '2c', '2d', '2s'; # but with all of the wilds in there
 
my %hits;
 
for my $i (0 .. $#deck-4) {
 
    my @subdeck = (@deck[$i..$i+4], '2c', '2d', '2s');
    push @subdeck, '2h' unless grep $_ eq '2h', @subdeck;
 
warn "subdeck: @subdeck\n";
 
    my $p = new Algorithm::Permute(\@subdeck, 5);
 
    while(my @res = $p->next) {
        my @sorted_hand = sort { sortspec($a) <=> sortspec($b) } @res;
        my $hand = join '', @sorted_hand;
        $hits{$hand}++;
    }
 
    print scalar(keys %hits), " hits\n";
 
}
 
my $total_hits = 0;
 
for my $k (keys %hits) {
    $total_hits++;
}
 
print "total hits: $total_hits\n";

-scott

Re:Duplicates...

btilly on 2009-02-18T01:00:08

Here is my solution to the problem.

There are 40 hands that you are trying to emulate. All of the straight flushes for 4 suits from ten low down to ace low. To avoid duplicates I will only count ways of representing the best possible hand. That means we can immediately ignore the 4 hands with a low card of 2 because the wild card can represent a 7 instead for a better hand. Let's segment possibilities by how many wild cards there are, whether you are representing one of the 4 royal flushes, the 28 straight flushes with a low of 3 through 9, and the 4 straight flushes with ace low. Note that in the case of a royal flush we can replace any combination of cards. In the 3-9 case we can replace any of the top 4 cards. In the ace low we cannot replace the ace and we must use a wild for the 2.

  • No wild cards, royal flush. There are 4 possibilities.
  • No wild cards, 3-9 low. There are 28 possibilities.
  • No wild cards, ace low. This means you have a 2, but 2s are wild, so this is impossible.
  • 1 wild card, royal flush. 4 represented hands, 5 possible cards replaced, 4 possible replacements, for 80 possibilities.
  • 1 wild card, 3-9 low. 28 represented hands, 4 possible cards replaced, 4 possible replacements for 448 possibilities.
  • 1 wild card, ace low. 4 represented hands, 1 possible cards replaced (the wild card has to be a 2), 4 possible replacements for 16 possibilities.
  • 2 wild cards, royal flush. 4 represented hands, 10 possible pairs replaced, 6 possible replacing pairs for 240 possibilities.
  • 2 wild cards, 3-9 low. 28 represented hands, 6 possible pairs replaced, 6 possible replacing pairs for 1008 possibilities.
  • 2 wild cards, ace low. 4 represented hands, 3 possible pairs replaced, 6 possible replacing pairs for 72 possibilities.
  • 3 wild cards, royal flush. 4 represented hands, 10 possible triplets replaced, 4 possible replacing triplets for 160 possibilities.
  • 3 wild cards, 3-9 low. 28 represented hands, 4 possible triplets replaced, 4 possible replacing triplets for 448 possibilities.
  • 3 wild cards, ace low. 4 represented hands, 3 possible triplets replaced, 4 possible replacing triplets for 48 possibilities.

Note that 4 wild cards is better as 5 of a kind instead of a straight flush.

So we have 4+28+0+80+448+16+240+1008+72+160+448+48 = 2552 possible hands.

This does not agree with your simulation. The two are off by 484. So where is the error? It is in confusion about what a straight flush is. To me a royal flush is just the best possible straight flush. But your simulation, and when I read more closely your root node, both indicate that you don't think that royal flushes should be counted as straight flushes. So get rid of the 4+80+240+160=484 royal flushes and our answers agree.

I hope for your sake that the Nevada gaming commission is willing to accept the simulation, because doing the math for, say, calculating full houses is beyond my patience. :-)

Re:Duplicates...

btilly on 2009-02-18T18:31:27

*sigh*. Full houses are easy. You can't have any wild cards because if you did you'd go for 4 of a kind instead. So you have 12 ways to pick the suit you're going to have 3 of a kind in, 4 choices of the cards in that kind, 11 ways to pick the one you have a pair in, and 6 choices for what the pair is. For 9504 possible full houses.

Much harder is 3 of a kind.

Re:Duplicates...

jmm on 2009-03-05T15:46:26

A full house with a wild card can happen. Two pair plus a wild card gives a full house. (Terminology nit: the three of a kind does not occur in a suit, but at a rank. 3 hearts and 2 spades is not a full house. :-)

Re:Duplicates...

btilly on 2009-03-05T16:06:32

I noticed that then decided not to reply to myself again just to point that out.

Re:Duplicates...

scrottie on 2009-02-19T19:31:02

I skimmed this to see that you came up with basically the same final result as me and then hurriedly moved on to other things, and then got stuck again. I'm still waiting for word on whether something without a big fat E's and ()'s is acceptable.

There's two things here... I mentioned this briefly before... but again...

1. How many combinations of the 52 cards are recgognized as that hand by itself

2. How many combinations of the 52 cards are recognized as that hand when other overlapping hands are tested first

Getting from #1 to #2 is a simple matter of subtracting. Technically, a royal flush is a special case of a straight flush and straight flushes include royal flushes, but the situation is kind of the opposite when the scoring test logic bumps the counter for "royal flush" but not he counter for "straight flush". Anyway, this isn't a big deal. Adding or subtracting to/from the output of the simulation to account for this works.

My best attempt, which wasn't good enough, was similar to that... tallying the results for each possible low card... but I should have gone further as you did and also break out how many wilds were in effect.

By the way, when I posted if anyone "wanted to do my work for me", I didn't mean "do my work while I get the money". I've also got just a few more of these and I do not want to do them =P Wanna drop me an email at scott@slowass.net?

-scott