File Exchange Pick of the Week

Our best user submissions


Brett's Pick this week is blackjack, by Cleve Moler.

Last night, while preparing for a seminar on machine learning I found myself with a lot of time to wait while a training algorithm ran. I was trying to create a classifier to differentiate images from 101 different categories, and things were taking a very long time.

A bit bored, I started browsing the File Exchange, and I came across a blackjack implementation by MathWorks's chief mathematician, chairman, and cofounder Cleve Moler. Intrigued, I downloaded it and spent the next hour happily playing cards while my computer churned away in the background.

The first thing I noticed when I ran Cleve's code was that, while it ran without errors, my Command Window was filled with red warnings that indicated that the 'symbol' font is no longer supported--as noted by user Bill R in 2015. (Cleve first submitted the file back in 2004.) The cards were readable, and they were properly colored, but the representations of the suits were awry. And though suits are irrelevant, the warning was annoying. Instead of suppressing the warning, I thought I'd take a quick stab at fixing it.

It took me a few seconds to find the offending line in Cleve's code:


...and to replace it with a simple indexing call to work around the issue:

charInd = [67,68,72,83];

In short order, I was playing again, this time with no warnings. (The replacement of the symbols with letters didn't bother me at all, and certainly didn't affect the game.)

Cleve's commentary in the file is also interesting; he provides some background information on the game, and a rudimentary approach to maximizing your likelihood of winning. (Or rather, to minimize your loss--without "counting strategies," the game is always slightly skewed in favor of the dealer, no matter how well you play--assuming you're not counting cards. And counting cards just doesn't work when a random number generator is dealing from a "continuous shuffling machine"!)

Armed with Cleve's strategy ("You stand on any total over 11 when the dealer is showing a six or less. Split aces and split 8's. Do not split anything else. Double down with 11, or with 10 if the dealer is showing a six or less."), I played 35 hands...and ended up $20 in the hole! If only there had been someone bringing me free drinks while I played, the experience would have been complete!

As it turns out, Cleve has blogged about blackjack before. The article makes for an interesting read--highly recommended. Among other things, I learned that, with a "basic strategy, the house advantage is only about one half of one percent of the original bet." (The article provides some more depth on strategy, and has some nice references.)

To test that "1/2-percent" idea, I turned to Cleve's companion code, blackjacksim, which provides a cummulative sum of n simulated runs of well-played blackjack hands. But instead of just simulating a million serial hands (which I did):

s = blackjacksim(1e6);
s(end)= -32025

(meaning that even if I played well, after a million hands I would be $32,000 in the hole!)...

I also simulated a million single hands, and looked at the distribution of 1-hand results (and the expected value of a single hand):

sv = zeros(1e6,1);
for ii = 1:1e6
   sv(ii) = blackjacksim(1);
ans =

(Pretty close to -1.5%!)


Thanks, Cleve. That was fun!

As always, I welcome your thoughts and comments.

Published with MATLAB® R2016b



To leave a comment, please click here to sign in to your MathWorks Account or create a new one.