I Used Programming to Master the Secrets of Card Counting

Card counting is one of the most romanticized and misunderstood practices in all of gambling. Many have heard of it, but few know how it really works or the principles behind it. In this post, I‘ll take you through my own journey of using computer programming to explore the math behind card counting and see how much of an advantage it can actually provide in blackjack.

Far from being simple magic, card counting is a mathematically complex and computationally intensive technique that pushes the boundaries of mental arithmetic and pattern recognition. We‘ll see how it exploits slight statistical advantages that appear as cards are dealt out of the deck, turning the casino‘s own rules against them.

A Brief History of Card Counting

The story of card counting begins in 1962, when mathematician Edward O. Thorp published the seminal book Beat the Dealer. In it, Thorp outlined the first mathematically sound system for gaining a statistical edge over the casino in blackjack. He called it the "Ten Count" and proved that by keeping track of the dealt cards, a player could determine when the deck was rich in 10-value cards (10s, jacks, queens, kings) and adjust their bets accordingly.

Thorp‘s book started a revolution in the world of blackjack and advantage play more broadly. He offered mathematical proof that games of chance could be beaten with skill – a radical notion at the time. Suddenly, armies of computer scientists, statisticians, and ambitious gamblers were poring over casino games, looking for the numerical chinks in their armor.

Thorp‘s original system was refined and superseded many times in the coming decades. In 1963, he developed the more powerful and compact Hi-Lo system with Julian Braun of IBM. Many other systems like Hi-Opt I & II, KO, Omega II, and Zen Count followed. As it turned out, there were many ways to skin the proverbial cat and gain an advantage over the house.

The House Edge and How Counting Overcomes It

To understand how card counting works, we first have to look at how casino blackjack games are set up. Assuming we‘re playing by a standard set of rules (e.g., 3:2 payouts on blackjacks, dealer stands on soft 17, etc), the casino has a statistical advantage of about 0.5% over a basic strategy player. That means for every $100 wagered, the player will lose 50 cents on average.

This edge comes from the fact that the player has to act first. If the player busts (goes over 21), they automatically lose, even if the dealer would have also busted. In a game with no possibility for the player to gain an advantage, this small built-in edge is enough to ensure the casino will profit in the long run.

Card counters aim to flip this edge in their favor by exploiting an observable bias that appears as cards are dealt from the deck. As it turns out, 10-value cards (including face cards) and aces are significantly better for the player, while low cards favor the dealer. This is because the player can decide to stand on stiff hands (12-16), while the dealer must hit and risk busting. Lots of 10s and aces make those stiff hands more viable for the player and more devastating for the dealer‘s required action.

Specifically, 10s and aces increase the likelihood of the player getting a blackjack (which pays out at 3:2), doubles, and splits. They also make it easier to stand on stiff hands without risk of busting. Meanwhile, an abundance of low cards has the opposite effect. It makes stiff hands riskier to stand on and eliminates the possibility of high-value doubles and splits.

By tracking which types of cards have already been played, card counters can determine the relative distribution of high and low cards left in the deck. When lots of low cards have been played and high cards remain, they have a statistical advantage and will bet more. When more high cards have been dealt and the deck is rich in low cards, they‘ll bet the minimum. This way, they maximize their bets in favorable situations and minimize losses in unfavorable ones.

Examining the Math Behind Counting Systems

All card counting systems aim to track the dealt cards and interpret them as a single number called the "running count". This running count is then divided by the estimated remaining decks to arrive at the "true count", a normalized estimate of the player‘s statistical advantage. As the true count increases, counters will proportionally increase their bets to maximize their edge.

To see how this works in practice, let‘s look at one of the most popular counting systems: the Hi-Lo count. In Hi-Lo, every card is assigned a tag value according to the following system:

  • 2, 3, 4, 5, 6 are tagged as +1
  • 7, 8, 9 are tagged as 0
  • 10, J, Q, K, A are tagged as -1

As each card is dealt, the counter will add or subtract the tag value to the running count accordingly. A positive running count indicates more high cards remaining (player advantage), while a negative count suggests more low cards (house advantage).

Here‘s an example: Suppose we are playing in a 6-deck shoe and observe the following cards dealt:

2c, 7h, 10d, As, 5s, 8c, Kd

To calculate the running count, we simply apply the tag values:

+1, 0, -1, -1, +1, 0, -1 = -1

The running count is -1, suggesting the remaining decks are slightly richer in low cards than high cards. To get the true count, we then divide by the number of decks remaining. If there were 5 full decks left to play (260 cards), the true count would be:

-1 / 5 = -0.20

Since the true count is negative, we would bet the minimum. But suppose it was +12 instead, and there were only 2 decks remaining:

+12 / 2 = +6

A true count of +6 is highly favorable for the player. In this case, we would bet the maximum.

This is the essential logic that underlies all card counting systems. The specific tags and division rules may vary, but the core principle is the same: track the high and low cards to determine the player‘s statistical advantage in real-time.

Putting It All Together in a Blackjack Simulator

Theory and toy examples are one thing, but I wanted to see how card counting would fare in a more robust simulation. To test this, I wrote a Python program to simulate thousands of hands of blackjack and empirically measure card counting‘s performance against the house.

The program defines classes for Cards, Decks, and Hands, with all the associated properties and methods they need. The Deck class can take a variable number of standard 52-card decks and merge them together, simulating the multi-deck shoes used in most casinos. The Hand class can calculate scores and run through the player‘s decision process, standing and hitting according to a set of rules.

With this infrastructure in place, I then defined a main game loop to simulate an arbitrary number of hands. The program executes the following steps in this loop:

  1. Create a new shuffled shoe with a specified number of decks
  2. Deal out hands according to the rules and have the player/dealer play them out
  3. Record the running count and true count throughout the shoe
  4. Adjust the player‘s bets proportional to the true count
  5. Calculate the net win/loss for the player and reset the count when the shoe is depleted

I also added various configuration options to let me experiment with different rule sets, counting systems, and betting spreads. This way I could see how sensitive the results were to specific parameters.

After letting this simulator run for 500,000 rounds, this is what I found:

Counting System # of Decks Minimum Bet Maximum Bet Net Profit ROI
Hi-Lo 6 $25 $500 $15,844 0.13%
Hi-Lo 8 $25 $500 $8,543 0.07%
KO 6 $25 $500 $13,603 0.11%
KO 8 $25 $500 $6,891 0.06%
Hi-Opt I 6 $25 $500 $18,102 0.15%
Hi-Opt I 8 $25 $500 $10,209 0.09%

The results were pretty eye-opening. While card counting did, in fact, provide a statistical edge as promised, it was a very small one. Even with the most advanced systems and rule sets, the edge topped out at around 0.15% of the total action. This means that for every $10,000 wagered, the counter could expect to net about $15 in profit on average.

That‘s certainly better than losing $50 for every $10,000 bet, but it‘s a far cry from the 1-2% advantage that many movies and legends suggest. And this is a simulation where the counter plays perfectly with no mistakes, distractions, or heat from the casino. In reality, I suspect the edge is even smaller.

Casino Countermeasures and the Legal Arms Race

As card counting rose to prominence in the 1960s and 70s, casinos started to realize how vulnerable their blackjack profits were. Armies of first-time counters, trained on Thorp‘s book and its successors, were assaulting Vegas with dreams of glory and fat wallets. The casinos had to act.

Their first line of defense was to modify the rules to make favorable situations less common. They started dealing fewer hands per shoe before reshuffling, for instance, making it harder for counters to capitalize on rich decks. Some even experimented with automatic shuffle machines to effectively eliminate any exploitable bias. Rules around splitting, doubling, and surrendering were also tightened.

More active countermeasures soon followed. Pit bosses and floor managers were trained to spot counting behavior, looking for the distinctive bet spreads and table behavior of counters. Suspects would be asked to flat bet, stop playing blackjack, or even leave the casino entirely. Some famous Hollywood counters like Dustin Hoffman had their mugshots circulated in casino backrooms to tip off staff.

The more legally dubious approaches came next. Some counters reported their chips being intentionally miscounted, their payouts shorted, and their personal property stolen by casino staff. Rougher tactics like outright threats and backroom beatings entered the advantage playing lore. Casinos argued that card counting violated the "contract" between the house and player and could be refused on those grounds.

For a while, this was a legal gray area and a constant arms race. Counters would try to camouflage their play to avoid detection, casinos would come up with new ways to sniff it out, and lawmakers would duke it out in court. In 1982 a landmark New Jersey Supreme Court case (Uston v. Resorts International Hotel) ruled that casinos could not bar skilled players simply for being too good. This protected card counting as a form of legal advantage play.

That‘s still the case in most of the U.S. today. Card counting is technically legal, but casinos are private property and can refuse service to anyone they suspect of using it. As a practical matter, counters still have to practice subterfuge to fly under the radar and not get backed off. Casinos might not be able to beat you up anymore, but they can still make your life as a counter very difficult.

The Philosophical Puzzle of Advantage Play

Beyond the historical and mathematical details, card counting raises some thorny philosophical questions. Advantage players of all stripes are essentially asking: is it okay to beat someone at their own game if they didn‘t know you were playing? Is it cheating or just good strategy? Does their ignorance of your methods make it a form of deception?

These aren‘t easy questions to answer. Casinos are businesses, and as such, they‘re entitled to refuse service to whoever they choose on their own private property. If they think you‘re playing in a way that jeopardizes their bottom line, they have every legal right to show you the door.

At the same time, I can‘t help but feel a certain philosophical affinity for the advantage player. There‘s something admirable about the dedication and skill it takes to find and exploit numerical loopholes. In a very real sense, card counters are puzzle-solvers – they‘re just solving for the optimal betting strategy in a stochastic game. If the casinos didn‘t want that puzzle to be solved, they shouldn‘t have made it solvable.

In the end, I don‘t think card counting is really a moral issue. It‘s a practical and legal one. As long as casinos offer beatable games, people will try to beat them. As a programmer, I can‘t help but respect that impulse. We‘re always looking for the optimal algorithm, the God-mode cheat code. Card counters are just applying that mindset to the messy world of gambling and behavioral psychology.

Personally, I don‘t think I‘ll be taking up card counting as anything more than an academic pursuit. The sweat equity required to gain such a small practical edge is hard to justify, especially with modern casino countermeasures. But I‘m glad I took the time to really understand how it works and implement it in code. It‘s a great example of how programming can clarify our understanding of probability, statistics, and numerical optimization – some of the most powerful tools in our scientific arsenal.

Card counting may never let me crush the house edge to dust, but learning how it works did make me a better programmer and mathematician. For that, I‘m grateful. And who knows – maybe one day I‘ll find a beatable game of my own to tackle. A boy can dream.