Simulating Blackjack


I’ve been a lit­tle bit inter­ested in black­jack of late and in card count­ing. It turns out that this is a topic about which it is easy to get infor­ma­tion. I sup­pose there is a huge mar­ket for advice for would be card coun­ters.

Black­jack is inter­est­ing in that it is the only casino card game in which it is pos­si­ble to beat the house edge through clever play, though to do so requires a lot of work. Essen­tial­ly, the rea­son is that a stan­dard game of black­jack already has a very thin edge for the house, only about 0.28% if you play an opti­mal strat­e­gy, 1 so if you can get a fur­ther edge you can beat it. Card count­ing lets you do this, but only be a very thin mar­gin.

I was con­sid­er­ing attempt­ing to fully model a black­jack game math­e­mat­i­cal­ly, but this would have been very labor inten­sive. Also, it’s been done a lot of times before. So, I thought it would be fun to instead sim­u­late a game of black­jack and com­pare var­i­ous strate­gies to see how much they lose or gain over time.

If all you know about black­jack is that you want to get as close to twenty one as pos­si­ble with­out going over, then the game is actu­ally much more com­pli­cated than you think, espe­cially when you attempt­ing to com­pute the odds. The first thing to know about black­jack is that you are play­ing against the deal­er. The sec­ond is that your goal is to get a higher hand than the deal­ers with­out going bust (get­ting a hand higher than twenty one.) Impor­tant­ly, you don’t know what the deal­er’s hand is going to be until you’ve played yours.

Essen­tial­ly, every game of black­jack begins with play­ers mak­ing bets and the dealer deal­ing two cards to every player at the ta­ble and two cards for him­self, with one of the dealer cards fac­ing down and the other fac­ing up. Each black­jack player then plays his hand until each is fin­ished. The dealer plays his hand last, turn­ing over all his cards, and then pay­ing out any bets. The game then starts again.

The value of a hand is the sum of all the cards in the hand. Each num­ber cards is worth it’s num­ber. Face cards are each worth ten and an ace is either one or eleven depend­ing on which makes the bet­ter hand. (Essen­tial­ly, an ace is an eleven only if that does­n’t make the hand bust. Only one ace can be worth eleven in any given hand.)

On each play­er’s turn, if he starts out with twenty one, that is, he has an ace plus a face or ten card, then he wins auto­mat­i­cally and receives a three to two pay­out on his bet. This is called ‘Black­jack.’ (Get­ting twenty one later is not called ‘black­jack,’ just ‘twenty one.’) If the player does­n’t get black­jack, he has a num­ber of options, the most impor­tant being ‘hit­ting,’ or ‘stand­ing.’ If the player ‘hit­s,’ then the dealer deals him another card and he may go again. If instead he ‘stand­s,’ the player is done and the dealer moves on the next play­er. There are sev­eral other moves, such as ‘split­ting’ on dou­bles, ‘dou­bling down,’ and ‘sur­ren­der.’ But the rules behind these vary from casino to casi­no. If at any point the play­er’s hand goes over twenty one, he is said to ‘bust,’ and he loses his bet.

It’s this bust­ing that gives the house the edge in black­jack. The dealer can bust too, but he can only do so after all play­ers have played their hands. So, if a player busts he loses his bet even if the dealer later busts. Since busts are com­mon, this means the player will lose rel­a­tively fre­quent­ly.

When it’s the deal­er’s turn, he must fol­low a strict strat­egy which is set by the house. Typ­i­cal­ly, what hap­pens is the dealer stands on any hand sev­en­teen or over and hits on and hand less. When the dealer plays, his final hand will always be between sev­en­teen and twenty one, or a bust.

I wrote a blackjack simulator which runs games fully func­tion­ally so that I can run games repeat­edly eas­ily to try dif­fer­ent strate­gies. I’ve made it so that I can set the num­ber of play­ers, the amount of money each player starts with, as well as the num­ber of decks the game will be played with. In addi­tion, I’ve allowed for the abil­ity to cre­ate arbi­trary strate­gies for each player hav­ing sep­a­rate han­dlers that can be passed in to rep­re­sent the play­er’s “AI.” Unfor­tu­nate­ly, the sim­u­la­tor does­n’t prop­erly model card count­ing sce­nar­ios. I could fix this but I feel like I’m done for now.

I’ve con­fig­ured to the black­jack sim­u­la­tor to com­pute the aver­age win­nings per hand for any given game. Run­ning a few hun­dred or a few thou­sand games I can gen­er­ate a his­togram which gives a good pic­ture of a given strat­e­gy. For exam­ple, if the player decides to fol­low the deal­ers strat­egy and stand on sev­en­teen, bet­ting ten every turn yields this return distribution:2

stand-on-17-histogram

You’ll see that the player loses an aver­age of about one dol­lar every hand with this strat­e­gy, which is about ten per­cent of his bet. It’s not very good but it’s much bet­ter than just play­ing randomly:

random-histogram

… where you can see that the player loses an aver­age of almost half his hand (of ten) every turn. You can find basic strate­gies to bet accord­ing to and I imple­mented one I found online, hard cod­ing it, so that it dou­bles down and splits at oppor­tune times. You’ll see that this strat­egy halves the loses I have from the stand on sev­en­teen strategy:

basic-strategy-histogram

This one is about twice as good as the stand of sev­en­teen strat­e­gy, but it is still a los­ing strat­egy over time. One final thing I tried was a sim­ple hi-low count­ing scheme. I imple­mented this as triv­ially as pos­si­ble so it just changes the bets on high and low counts rather than chang­ing the actual play strat­e­gy. It turns out that this is actu­ally not an improve­ment over basic play:

card-counting-histogram

If I run the basic strat­egy and the basic strat­egy plus card-­count­ing back to back each with the same seeded deck and I count aver­age the loses and wins for both of them, I find that card count­ing like this actu­ally causes me to lose slightly more:

basic-strategy: -0.541545
basic-strategy+card-counting: -0.66769165

Of course, I’m doing two things wrong here with regard to card count­ing. The first is that I’m not count­ing enough cards. It’s tricky to count only the cards the player can see and I did­n’t model that cor­rectly when I wrote this sim­u­la­tor. That can be fixed but I haven’t both­ered yet. The other thing is that I’m not adjust­ing the play strat­egy accord­ing to card counts. The rea­son is I believe that I need a bet­ter under­stand­ing of black­jack than I have to do that cor­rect­ly. I may some­time do that, but I’m a lit­tle tired of this project for now.

  1. according to stats I found online, 
  2. Unless stated otherwise, these graphs are from sets of 1000 games with 100o hands each, and a deck with two packs of cards. 

Last update: 05/11/2013

blog comments powered by Disqus