# Python Blackjack Simulator

Python Blackjack SimulatorTo hit, or not to hit?Anmol SrivatsBlockedUnblockFollowFollowingMay 19BackgroundA computer is an ideal tool to examine the interaction between statistics and strategy.

This is because while strategic problems tend to be theoretical, they are also meant to have real world consequences, and these are reflected through statistical outcomes.

In many cases, the statistical outcomes are too difficult to study by hand, so it is easier to do a ‘Monte Carlo simulation’, which is several random trials, to try and tease out patterns.

Poker is an extremely complicated game, there are over ten trillion possible hands of poker between two people.

This means that current computers have nowhere near enough power to ‘solve’ poker, so they have to frequently use clever Monte Carlo methods.

I decided to start off a little simpler, and try to do this for Blackjack.

A special thanks to wizard of odds, whose chart I used to design my strategy function.

The code used for this project is on my Github, as it is too long to paste here.

Rules of BlackjackBlackjack is a game which a player plays against a casino.

Both the player and the casino try to get cards that add up to as high a number as possible without crossing 21.

Crossing 21 means you automatically lose.

If both the player and the casino both cross 21, the casino wins.

However, the player can see one of the casino’s cards before making their decision, whereas the casino acts independently of the player’s cards, generally continuing until its score hits 17.

Each numerical card (2–10) is worth its number, an ace is worth 1 or 11, a face card is worth 10.

Note that this means there are effectively 4 times as many 10s (10, J, Q,K) as other cards.

There are various other caveats, like blackjack bonuses, splits, and doubles, which you can read about at this site.

Various blackjack outcomes and their associated resultsProject OverviewThis project had three phases:Make a function to give you strategic blackjack advice , i.

e.

whether to hit, stay, etc.

Make a blackjack simulator to simulate one hand of blackjack using the above strategyRun the simulation multiple times to aggregate results and run experimentsPhase 1: StrategyBlackjack is a game that has been studied quite a lot, and there are various places on the internet that have the perfect strategy, which is known as ‘Basic blackjack strategy’.

Before you make retirement plans, note that even if you play ‘perfect’ blackjack, you are losing at a small rate vs the casino (around 0.

5%), but are just losing slower than people who play imperfect blackjack.

The information is usually displayed in a grid-like chart.

Here is a snippet of the chart I used for this project:If my cards add up to 12, and the dealer has a 7, the chart says I should ‘hit’, i.

e.

take an extra card.

However, if the dealer has a 5, I should ‘stay’, i.

e.

give up, and hope the dealer goes ‘bust’ (scoring above 21).

I encoded every recommendation of the chart into a function, BlackJackStrategy.

Phase 2: SimulateTrying to simulate each hand of blackjack was by far the most challenging part of this project.

I first created a random deck with all the numbers, and four times as many tens.

Then, using a random number generator, I dealt the player two cards, and the dealer one card, and used basic blackjack strategy to decide how the player would act, and determined the outcome using the rules of blackjack.

This is all inside the function ‘blackjack_hand_result’.

Note: I use a simulation that assumes an infinite number of decks of cards, since casinos often use 6–8 decks, this should not change the results too much.

I simulate the results of ‘splits’ as two different hands, which allowed me to deal with splits recursively.

This will reduce the variance a little bit, but by a negligible amount.

Phase 3: Repeat Simulations, and ExperimentRepeating the simulations was by far the easiest part of the project, just repeating my blackjack hand function how many ever times I wanted.

My first experiment was to see what the the ‘house edge’ was in BlackJack, which is the amount of money you would expect to lose for every \$100 you wager.

I tried this by simulating a million hands, a few times, and found that you will lose somewhere between 10 and 40 cents.

Experiment 1: Doubling 7 vs dealer 6On the basic blackjack strategy chart, it says to hit on a 7 vs a dealer’s 6.

What happens if we double instead?.I added a ‘hard code’ input to my one hand blackjack simulator, which allows you to hard code the player’s first action, and then assume the player follows basic blackjack’s recommendation for the rest of the hand.

I first ran 100,000 simulations of a player betting \$1, and having a 3 and a 4 vs a dealer’s 6, the player MADE around \$2,000.

However, when I ran 100,000 simulations where double was hard coded, this changed to a LOSS of \$15,000.

This means that the decision to double a 7 vs a 6 cost the player \$17,000 out of \$100,000 wagered, or 17% of their bet on average!Experiment 2: Splitting two 10s vs dealer 6‘Never split tens’ is a famous piece of blackjack advice.

Let’s evaluate it by seeing what happens if you split with two 10s vs the dealer having a 6, which is the worst card for the dealer.

When we run 100,000 simulations without splitting, the player makes around \$70,000.

However, when we hard code ‘Split’ in, the player makes only \$60,000.

While having two 10s vs a 6 is still immensely profitable, it costs the player 10% of their bet to split 10s.

ConclusionIt is possible to test every single aspect of basic blackjack strategy by changing the hard coding and seeing which method works best in the simulations.

It was quite fun to code and test, and I look forward to applying similar methods when studying other strategy problems.