http://www.perlmonks.org?node_id=603907


in reply to Optimising combinatorial iterations by filtration

Maybe this will help you generate a lookup table.

The number of truly different five card poker hands is not that large. Brute force is good enough to list all 7462 of them.

First, forget about suits, and generate all combinations of ranks. Sort them and join them in a string. Count the number of unique ranks in the hand. If it is one, the combination is no good (five aces). If it is five, a flush is possible.

Rank all hands from 0..7461 and you have a quick lookup.

Update:
Deal a few hundred random outcomes and tally the results.

YuckFold

#!/usr/bin/perl use strict; my @chars = 'a'..'m'; my %hands; for my $c0 (@chars) { for my $c1 (@chars) { for my $c2 (@chars) { for my $c3 (@chars) { for my $c4 (@chars) { my $list = [$c0, $c1, $c2, $c3, $c4]; my $uniq = unique_ranks($list); my $key = join('', sort @$list ); # is a valid hand if ($uniq > 1) { $hands{$key}++; } # can be flushed if ($uniq > 4) { $hands{"$key+"}++; } } } } } } for my $key (sort(keys(%hands))) { $key =~ tr/a-m/AKQJT98765432/; print "$key\n"; } #----------------------------------------------------------- sub unique_ranks { my $list = shift; my %uniq = map { ($_ => 1) } @$list; return keys %uniq; }

Replies are listed 'Best First'.
Re^2: Optimising combinatorial iterations by filtration
by Moron (Curate) on Mar 12, 2007 at 13:34 UTC
    Your ideas are truly excellent. I will need to work on it to be able to apply this at different stages in the hand lifecycle (pre-flop, flop, turn, river), but, very well done anyway - you have found a good optimisation strategy indeed!

    Update: Just spotted one hole that I thought had been covered in this but seems to be there after all. For example, there are 48 ways to get four aces but only 20 7-high flushes. Therefore a holding of say full house has to take into account the number different ways to land on each rank above or below it or at least take into account the cumulative sum of all possible ways up to current rank and divide that by the (constant and known) number of all possible combinations.

    But I could for example calculate the number of possible hands at each rank by a cut-down combination operation and feed that in instead of doing your ++ iterative approach - this changes things a bit so I need to do further work on this algorithm even so.

    More update - actually that wasn't quite the right example. I should have been talking about exact rank, i.e. for identical hands. Here is a better one: There are only four combinations (and 480 permutations) of 7 5 4 3 2 suited, but there are 1020 combinations (and 122400 permutations) of the same hand unsuited. These might be unique by the reduced iteration but do not therefore have equal likelihood.

    -M

    Free your mind