Code Throwdown: Berg Poker Simulator

Four-high flush
A four-high flush, one of three winning flush hands that are only seen in poker games played
with multiple decks of cards, regardless of what wildcards are used.

It all started with what seemed like a simple question. In a poker game with eight players, using two decks of cards, with five card hands and fives wild, what are the odds for each type of hand? What about seven card hands with sevens wild? What hands should you play and what should you fold?

There are a number of ways to calculate the odds in poker games. For most traditional games, they have been computed manually long ago. You can also enumerate all possible hands. But adding in wildcards and multiple decks makes the calculation harder. The most practical approach is just to simulate the game and play a number of hands, the odds are easily determined if you play a few million hands.

If you allow for multiple decks and wildcards, there are 380,204,032 permutations possible with 5 cards. There are 12,051 distiguishable hands. With a single deck there are only 7,462 distinct 5-card hands. Here are similar lists for 5-card single deck deuces wild, 7-card single deck and 7-card single deck deuces wild.

The core of any poker simulation program is the poker hand evaluator function. Given a hand, it produces a number representing the evaluation of the hand, with higher numbers denoting better hands. There are a number of poker hand evaluators out there, see The Great Poker Hand Evaluator Roundup, Moritz Hammer's evaluator, PokerAI's 7-card evaluator contest, Cactus Kev’s 5-Card Evaluator, and SpecialK's 7-card evaluator. I decided to see if I could write a poker hand evaluator that could handle multiple decks and wildcards. The current version of the simulator can shuffle, deal and evaluate more than 22 million seven card hands per second on my six-year-old PC, this was measured by testing the seven card game below for a billion hands. The evaluator can evaluate hands of any size, the interface supports up to twenty cards in a hand.

Most poker hand evaluators use large lookup tables. The layered cache architecture of modern CPUs means that doing any lookup in a big table will thrash your cache and with wildcards or multiple decks, the lookup tables quickly become too large. I wrote a poker hand evaluator that does not sort the hand or use large tables. Maintaining code and data locality produced amazing speed gains. My goal was to write an evaluator that could run without requiring the enormous memory footprint that some of the really fast lookup methods have. More importantly, the evaluator supports arbitrary wild cards, multiple decks of cards, jokers, the addition or removal of arbitrary cards, and hands of any size. This means you can simulate games that single-deck no-wilds evaluators cannot handle. The resulting simulator runs so fast that it can be used in real time while using less than a megabyte of memory.

I added a user interface and called it the Berg Poker Simulator. The simulator runs from the command line of any Windows PC. Here is how we use it to answer the question that started all of this:

Berg Poker Simulator v1.4.5 (Copyright (C) 2013 Henry Berg, non-commercial use only)

bpsim>players 8
Using 8 players.
bpsim>decks 2
Using 2 decks.
bpsim>handsize 5
Hand size now 5 cards.
bpsim>wild 5
Set wild 5
bpsim>go

Poker with 2 decks, 0 jokers, 8 players, hand size 5 cards.
Wildcards: Five
No added cards.
No removed cards.
Simulating 5000000 hands played.
Simulation time 1.234 seconds.

HAND            WINS   ACE  KING QUEEN  JACK   TEN  NINE EIGHT SEVEN   SIX  FIVE  FOUR THREE   TWO
----            ----   ---  ---- -----  ----   ---  ---- ----- -----   ---  ----  ---- -----   ---
High Card         1%    1%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
One Pair          2%    9%    4%    2%    1%    1%    1%    1%    1%    1%    0%    1%    1%    1%
Two Pair         14%   16%   15%   14%   14%   13%   13%   12%   12%   12%    0%   11%   11%    0%
Three of a Kind  42%   70%   61%   54%   48%   43%   38%   34%   31%   27%    0%   25%   22%   20%
Straight         78%   82%   80%   78%   77%   75%    0%   75%   74%   74%   73%    0%    0%    0%
Flush            83%   84%   78%   78%   78%   77%   78%   78%   78%   75%    0%   22%    0%    0%
Full House       86%   89%   87%   87%   86%   86%   85%   84%   84%   84%    0%   84%   83%   82%
Four of a Kind   95%   99%   98%   98%   97%   96%   95%   94%   93%   93%    0%   92%   91%   90%
Straight Flush   99%   99%   99%   99%   99%   99%    0%   99%   99%   99%   99%    0%    0%    0%
Five of a Kind   99%  100%  100%   99%   99%  100%   99%   99%   99%   99%    0%   99%   99%   99%

This test was run on a Windows XP machine with an Intel Q6600 @ 2.4GHz. Note that it shuffled, dealt and evaluated hands for eight players playing 5 million hands each, which was overkill. Simulation of all 40 million hands required 1.234 seconds – more than 32 million hands per second. The output shows the probability of victory for each type of hand and each specific high card variant of that hand. The "break" hand for this game, where you have more than a 50% chance of winning, is three queens, which wins 54% of the time if everybody stays in.

What about the seven card question?

Berg Poker Simulator v1.4.5 (Copyright (C) 2013 Henry Berg, non-commercial use only)

bpsim>players 8
Using 8 players.
bpsim>decks 2
Using 2 decks.
bpsim>handsize 7
Hand size now 7 cards.
bpsim>wild 7
Set wild 7
bpsim>go

Poker with 2 decks, 0 jokers, 8 players, hand size 7 cards.
Wildcards: Seven
No added cards.
No removed cards.
Simulating 5000000 hands played.
Simulation time 1.796 seconds.

HAND            WINS   ACE  KING QUEEN  JACK   TEN  NINE EIGHT SEVEN   SIX  FIVE  FOUR THREE   TWO
----            ----   ---  ---- -----  ----   ---  ---- ----- -----   ---  ----  ---- -----   ---
High Card         0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
One Pair          1%    1%    1%    1%    1%    1%    1%    0%    0%    0%    0%    0%    0%    0%
Two Pair          1%    1%    1%    1%    1%    1%    1%    1%    0%    1%    1%    1%    1%    0%
Three of a Kind   1%    3%    3%    2%    2%    2%    1%    1%    0%    1%    1%    1%    1%    1%
Straight          9%   14%   10%    8%    0%    8%    7%    7%    7%    5%    4%    0%    0%    0%
Flush            18%   20%   11%   11%   10%   10%   11%   10%    0%   11%   10%   12%    0%    0%
Full House       34%   47%   42%   38%   35%   32%   30%   28%    0%   26%   24%   23%   21%   20%
Four of a Kind   71%   89%   84%   81%   77%   74%   71%   67%    0%   65%   62%   59%   56%   54%
Straight Flush   94%   96%   95%   94%    0%   94%   93%   93%   92%   91%   91%    0%    0%    0%
Five of a Kind   98%  100%   99%   99%   99%   99%   98%   98%    0%   98%   97%   97%   97%   96%

This time we shuffled, dealt, and evaluated 40 million seven card hands in 1.796 seconds – more than 22 million hands per second. Now we see that the odds have shifted, so three queens now has 2% chance. The break hand for this game is four twos, which has a 54% shot.

The evaluator used by the simulator can evaluate any size poker hand. Just for fun, let's ask it something harder. Four of you sit down and deal out an entire deck of cards, so each person has 13 cards in their hand. Assuming you can even figure out what you have, what the heck is a good hand?

Berg Poker Simulator v1.4.5 (Copyright (C) 2013 Henry Berg, non-commercial use only)

bpsim>players 4
Using 4 players.
bpsim>decks 1
Using 1 deck.
bpsim>handsize 13
Hand size now 13 cards.
bpsim>go

Poker with 1 deck, 0 jokers, 4 players, hand size 13 cards.
No wildcards.
No added cards.
No removed cards.
Simulating 5000000 hands played.
Simulation time 1.766 seconds.

HAND            WINS   ACE  KING QUEEN  JACK   TEN  NINE EIGHT SEVEN   SIX  FIVE  FOUR THREE   TWO
----            ----   ---  ---- -----  ----   ---  ---- ----- -----   ---  ----  ---- -----   ---
High Card         0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
One Pair          0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
Two Pair          1%    1%    1%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
Three of a Kind   0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%
Straight          1%    1%    1%    1%    1%    1%    1%    1%    1%    1%    1%    0%    0%    0%
Flush             6%   11%    4%    2%    1%    1%    1%    1%    1%    0%    0%    0%    0%    0%
Full House       41%   84%   71%   61%   53%   45%   39%   33%   28%   24%   21%   18%   15%   13%
Four of a Kind   89%   94%   93%   93%   93%   92%   91%   90%   89%   88%   87%   86%   84%   82%
Straight Flush   97%  100%   99%   99%   98%   98%   97%   96%   95%   94%   94%    0%    0%    0%
Five of a Kind    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%    0%

This time we shuffled, dealt, and evaluated 20 million thirteen card hands in 1.766 seconds – more than 11 million hands per second, even with thirteen cards to consider! If you play this game, don't go in with less than a jacks-over full house (wins 53% of the time) unless you are bluffing.

The tool supports adding and removing cards, selecting wildcards or adding jokers, and handles hand sizes from one to twenty cards. Hands with fewer than five cards ignore flushes and straights. It has been tested on Windows XP and Windows 7, and it is a standard 32-bit application written in C++ that should run fine on almost any Windows PC. It is free to try for non-commercial use, i.e. determining the odds for that poker game you play with your golf buddies:

Download Berg Poker Simulator (version 1.4.5) (zip file)

If you find this tool useful, please consider making a donation of what you think it is worth.

Bitcoin donations accepted at address bc1qynhyk0kl50vjcl8fxsn0uaesq2vdc940tkyx62:

bc1qynhyk0kl50vjcl8fxsn0uaesq2vdc940tkyx62

If you have questions or are interested in licensing the source code for commercial use, please contact me at henry.berg@codethrowdown.com.

Thanks,
Henry Berg
April 29, 2022

P.S. This site is called Code Throwdown because the very early versions of the simulator came from a programming duel with a friend.

Berg Poker Simulator and this web page Copyright (C) 2022 Henry Berg