# Studying Blackjack (#151)

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message.

2. Support Ruby Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The majority of the strategy in Blackjack hinges around the dealer’s
hand. The
reasons are likely obvious to most of you: that’s the hand you have to
beat and
the dealer plays by fixed rules we can predict.

For those unfamiliar with Blackjack, you only need to know a tiny bit
game for the purposes of this exercise. The goal for both the player
and the
dealer is to draw cards to make a hand with the highest total possible,
without
going over 21. Going over 21 is called “busting” and it means you lose
the
hand. Face cards count for ten, aces are one or eleven (whichever is
better for
the hand), and all other cards count for their face value. You start
with two
cards and, if they happen to be a ten valued card and an ace (a count of
21),
the hand is called a “natural.” A natural is an automatic win in most
cases.

The dealer begins with one of his two cards face up and one face down.
We call
the former the “upcard.” The dealer will “hit” or take more cards until
he
reaches a count of 17 or higher. After that he will “stand” or leave
the hand
where it is. That tells us that there are only seven possible outcomes
for the
dealer: get dealt a natural, bust, or hit to a total of 17, 18, 19, 20,
or 21.

We start every hand knowing half of what the dealer holds thanks to the
upcard.
Believe it or not, you can make pretty reliable guesses about how the
hand will
go with just that knowledge.

Write a Ruby program that shows the percent chance of a dealer reaching
each
possible outcome based on the upcard showing.

I’ll give you some hints to verify your results. Basic Blackjack
strategy
teaches that we should assume the dealer “has a ten in the hole” (as the
face
down card). It’s not always true, of course, but 17 is a common outcome
for a
dealer with an upcard of seven. Finally, we call five and six “the
dealer’s
bust cards” for reasons that will become obvious if you are outputting
correct
percentages.

In the casinos Blackjack is often played with more than one deck
shuffled
together. One, two, six, and eight deck games are common. You may want
to
offer the option to adjust the deck size your program uses. Either way,
let’s
default to two decks as an average of what a player will face.

Write a Ruby program that shows the percent chance of a dealer
reaching each
possible outcome based on the upcard showing

Are we able to post partial / full outcomes within the 48 hours?

Cheers,
Dave

On Jan 4, 2008, at 4:27 PM, Sharon P. wrote:

Write a Ruby program that shows the percent chance of a dealer
reaching each
possible outcome based on the upcard showing

Are we able to post partial / full outcomes within the 48 hours?

You bet. Feel free.

James Edward G. II

Are we able to post partial / full outcomes within the 48 hours?
You bet. Feel free.

Here’s mine so far, my first ruby quiz !

Upcard Bust 17 18 19 20 21 Natural
c1 26.20% 16.80% 13.00% 14.20% 13.90% 15.90% 0.00%
c2 38.80% 14.60% 11.40% 11.00% 12.30% 11.90% 0.00%
c3 40.50% 14.40% 13.40% 11.20% 12.00% 8.50% 0.00%
c4 38.00% 15.20% 14.30% 11.70% 10.70% 10.10% 0.00%
c5 42.30% 12.80% 12.30% 11.80% 9.20% 11.60% 0.00%
c6 39.10% 23.50% 10.30% 10.00% 8.80% 8.30% 0.00%
c7 25.60% 35.40% 14.00% 10.70% 6.50% 7.80% 0.00%
c8 23.70% 13.00% 33.60% 16.20% 6.20% 7.30% 0.00%
c9 21.90% 10.50% 11.90% 35.90% 12.90% 6.90% 0.00%
ct 19.40% 12.50% 10.90% 11.20% 31.70% 5.10% 9.20%
cj 19.70% 14.40% 10.60% 12.30% 29.90% 5.80% 7.30%
cq 18.90% 13.50% 10.90% 10.30% 32.20% 6.60% 7.60%
ck 19.50% 12.30% 12.80% 11.10% 31.90% 6.00% 6.40%
ca 14.00% 13.40% 14.40% 13.10% 12.60% 4.10% 28.40%

Seems to satisfy the hints given in the quiz, but I’m still not sure
if I deal with the case where the dealer gets dealt two or more aces
correctly.

Chris

I didn’t think it was supposed to be run a specific number of times,
but go over all possibilities.

I was thinking something like this: (with 2 decks of cards)
2 2 2 2 2 2 2 2 3 - 19
2 2 2 2 2 2 2 2 4 - 20
2 2 2 2 2 2 2 2 5 - 21
2 2 2 2 2 2 2 2 6 - bust
2 2 2 2 2 2 2 2 7 - bust

also would these be considered different hands or the same?
2 2 2 2 2 2 2 2 3
2 2 2 2 2 2 2 3 2

I may be wrong though. If that’s the case someone correct me.

Joe

On Jan 4, 2008, at 8:22 PM, Joe wrote:

also would these be considered different hands or the same?
2 2 2 2 2 2 2 2 3
2 2 2 2 2 2 2 3 2

I may be wrong though. If that’s the case someone correct me.

I think I’ve heard about two different, but very interesting
strategies to solve this problem. I can’t wait to see the code.

James Edward G. II

Here are my results. I combined the 10 and the face cards in one row
since they’re essentially equivalent. And the data was generated by
doing 10,000 deals per upcard.

upcard bust 17 18 19 20 21
natural

`````` 2   |   35.95%   13.32%   13.17%   13.34%   12.44%   11.78%
``````

0.00%
3 | 37.85% 13.41% 12.93% 11.69% 12.51% 11.61%
0.00%
4 | 40.14% 13.24% 11.86% 12.03% 11.48% 11.25%
0.00%
5 | 41.80% 12.82% 12.32% 11.41% 11.34% 10.31%
0.00%
6 | 41.76% 17.65% 10.59% 10.36% 10.04% 9.60%
0.00%
7 | 26.77% 36.23% 13.53% 8.29% 7.88% 7.30%
0.00%
8 | 24.96% 12.84% 35.69% 12.73% 7.04% 6.74%
0.00%
9 | 22.98% 12.27% 10.69% 35.47% 12.22% 6.37%
0.00%
10 | 21.61% 11.21% 11.68% 11.23% 32.97% 3.55%
7.75%
ace | 11.60% 12.33% 13.03% 13.06% 13.43% 5.27%
31.28%

I do wonder, though, whether the deck is biased somehow when the
dealer deals him/herself since all the players are dealt beforehand.
For example, it seems a player is more likely to consume a sequence of
low cards through consecutive hits than a sequence of high cards. So
depending on the number of players at the table, the cards the dealer
is likely to get may change somewhat. And perhaps the dealer’s odds
change further the deeper into the shoe the table gets. Does anyone
know?

Eric

====

Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com

On Jan 4, 2008, at 7:55 PM, Eric I. wrote:

I do wonder, though, whether the deck is biased somehow when the
dealer deals him/herself since all the players are dealt beforehand.

I believe what you say is true, though I think it’s a detail typically
ignored in this kind of strategic analysis.

Google the “cut card effect blackjack” for some interesting (and I

James Edward G. II

On Jan 4, 2008, at 6:35 PM, Chris wrote:

Are we able to post partial / full outcomes within the 48 hours?
You bet. Feel free.

Here’s mine so far, my first ruby quiz !

Welcome to the quiz. Glad to have you.

…but I’m still not sure if I deal with the case where the dealer
gets dealt two or more aces correctly.

If a dealer has two aces, the first one would be 11 and the second one
a 1 for a total of twelve. Any aces added into the mix would be
another one. The original aces would drop to a one if keeping it at
11 takes the dealer over 21.

James Edward G. II

Write a Ruby program that shows the percent chance of a dealer
reaching each possible outcome based on the upcard showing.

I’m sorry for asking this dummy question (and I hope not to spoil it
for
anyone) but all this jargon makes me feel strange and it’s early in
the
morning. Do I understand the game right in that:

``````1. The dealer gets a card
2. If the sum of his cards is < 17, repeat step #1
3. If the sum is >= 17, the result is registered
4. If the dealer has 6 + Ace, he stops and doesn't try to get a 4
``````

or
something lower?

The set of possible cards is limited to 2 decks, i.e. 2 * 13 (2…10,
B,
D, K, A) cards by default.

A set of two card decks contains two 2s?

The probabilities for 10, B, D, K should be all the same since they
all
count 10 and removing them from the card set results in the same
probability
to get another card with a value of 10 for each of them?

On 4 Jan 2008, at 22:31, James G. wrote:

James Edward G. II

Here’s my results so far. I don’t completely trust them yet since I’m
not sure I understand the blackjack rules properly (I’m assuming the
dealer has to use an ace as 11 unless they bust).

/dh

\$ ./blackjack.rb
Odds for each dealer outcome based on initial upcard (2 deck game)
17 18 19 20 21 BUST
A 12.58% 12.82% 12.75% 12.85% 36.07% 12.93%
2 13.93% 13.33% 13.07% 12.39% 11.92% 35.36%
3 13.27% 13.06% 12.45% 12.18% 11.53% 37.50%
4 13.07% 12.02% 12.10% 11.63% 11.31% 39.88%
5 12.10% 12.28% 11.73% 10.90% 10.73% 42.25%
6 16.62% 10.62% 10.67% 10.12% 9.75% 42.21%
7 37.05% 13.82% 7.80% 7.88% 7.34% 26.11%
8 12.97% 36.12% 12.90% 6.89% 6.96% 24.16%
9 12.09% 11.20% 35.41% 12.11% 6.10% 23.09%
10 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
J 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
Q 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
K 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%

The set of possible cards is limited to 2 decks, i.e. 2 * 13 (2…10,
B,
D, K, A) cards by default.

It’s * 4 because there are 4 colours.

Hi Chris,

You have an extra card in your hand (a ‘1’ as well as an ace).

/dh

You have an extra card in your hand (a ‘1’ as well as an ace).

Thank you, well spotted !

Chris

On Jan 5, 2008 12:25 PM, Denis H. [email protected] wrote:

Here’s my results so far. I don’t completely trust them yet since I’m
not sure I understand the blackjack rules properly (I’m assuming the
dealer has to use an ace as 11 unless they bust).

I think that’s correct.

5 12.10% 12.28% 11.73% 10.90% 10.73% 42.25%
6 16.62% 10.62% 10.67% 10.12% 9.75% 42.21%
7 37.05% 13.82% 7.80% 7.88% 7.34% 26.11%
8 12.97% 36.12% 12.90% 6.89% 6.96% 24.16%
9 12.09% 11.20% 35.41% 12.11% 6.10% 23.09%
10 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
J 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
Q 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
K 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%

Those last four lines are identical. Are you calculating the
probabilities and not simulating, or do you just output the last run
four times?

Here are my results, 2 decks and 1_000_000 deals each.

\$ ruby1.9 blackjack.rb
upcard: 2
17 18 19 20 21 bust
13.9% 13.4% 13.1% 12.4% 11.9% 35.3%
upcard: 3
17 18 19 20 21 bust
13.3% 13.1% 12.5% 12.2% 11.5% 37.4%
upcard: 4
17 18 19 20 21 bust
13.1% 12.0% 12.1% 11.7% 11.3% 39.8%
upcard: 5
17 18 19 20 21 bust
12.1% 12.3% 11.7% 10.9% 10.7% 42.1%
upcard: 6
17 18 19 20 21 bust
16.6% 10.7% 10.7% 10.1% 9.8 % 42.1%
upcard: 7
17 18 19 20 21 bust
36.8% 13.9% 7.9 % 8.0 % 7.3 % 26.1%
upcard: 8
17 18 19 20 21 bust
13.0% 36.0% 12.9% 6.9 % 7.0 % 24.3%
upcard: 9
17 18 19 20 21 bust
12.1% 11.2% 35.3% 12.2% 6.1 % 23.1%
upcard: 10
17 18 19 20 21 bust
11.3% 11.2% 11.3% 33.6% 11.3% 21.3%
upcard: 11
17 18 19 20 21 bust
12.6% 12.8% 12.7% 12.9% 36.0% 13.0%

You have an extra card in your hand (a ‘1’ as well as an ace).

Here’s my updated results using a two-deck deck and 5000 simulated
hands per upcard.

Upcard Bust 17 18 19 20 21 Natural
2 35.14% 13.66% 13.80% 13.18% 12.10% 12.12% 0.00%
3 37.96% 13.02% 12.72% 12.98% 12.10% 11.22% 0.00%
4 39.98% 13.40% 12.70% 11.70% 11.58% 10.64% 0.00%
5 40.70% 12.86% 12.66% 11.12% 11.94% 10.72% 0.00%
6 43.02% 16.74% 10.30% 10.20% 9.78% 9.96% 0.00%
7 26.36% 37.44% 13.18% 8.02% 7.84% 7.16% 0.00%
8 24.52% 12.68% 35.78% 13.42% 6.72% 6.88% 0.00%
9 23.64% 11.52% 11.98% 34.42% 11.88% 6.56% 0.00%
t 21.60% 11.60% 10.90% 11.22% 33.36% 3.98% 7.34%
j 21.30% 11.84% 10.94% 11.68% 32.84% 3.88% 7.52%
q 21.78% 11.26% 10.94% 11.20% 34.08% 3.68% 7.06%
k 20.60% 10.96% 10.34% 11.82% 34.60% 3.54% 8.14%
a 12.48% 13.40% 13.20% 12.82% 12.86% 4.84% 30.40%

Chris

On 5 Jan 2008, at 13:45, Sander L. wrote:

9 12.09% 11.20% 35.41% 12.11% 6.10% 23.09%
10 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
J 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
Q 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%
K 11.29% 11.22% 11.30% 33.56% 11.31% 21.32%

Those last four lines are identical. Are you calculating the
probabilities and not simulating, or do you just output the last run
four times?

I’m calculating (based on every permutation of remaining cards) rather
than simulating. Hence I only do one run. I guess I could have
duplicated the last four lines and saved a few CPU cycles but it runs
reasonably fast anyway so I didn’t bother.

Here are my results, 2 decks and 1_000_000 deals each.

I’m guessing that you used a simulation approach instead. It’s
interesting, and reassuring, that we got the same results.

/dh

On Jan 5, 2008, at 2:05 AM, tho_mica_l wrote:

Write a Ruby program that shows the percent chance of a dealer
reaching each possible outcome based on the upcard showing.

I’m sorry for asking this dummy question (and I hope not to spoil it
for
anyone) but all this jargon makes me feel strange and it’s early in
the
morning.

No worries. I probably didn’t explain it well.

Do I understand the game right in that:

1. The dealer gets a card
2. If the sum of his cards is < 17, repeat step #1
3. If the sum is >= 17, the result is registered
4. If the dealer has 6 + Ace, he stops and doesn’t try to get a 4
or
something lower?

Yes to all of the above.

The forth rule actually varies somewhat in the casinos. Some tables
allow a dealer to hit on “soft 17” (a total of 17 involving an ace
used as an eleven). I left that out to keep this exercise simple.

The set of possible cards is limited to 2 decks, i.e. 2 * 13 (2…10,
B,
D, K, A) cards by default.

A set of two card decks contains two 2s?

I meant to do the experiment using two full decks shuffled together.
That would be a total of 104 cards and include eight twos, threes, etc.

I hope that clears things up.

James Edward G. II

On Jan 5, 2008, at 5:25 AM, Denis H. wrote:

(I’m assuming the dealer has to use an ace as 11 unless they bust).

Correct.

James Edward G. II

On Jan 4, 2008 11:37 PM, James G. [email protected] wrote:

Google the “cut card effect blackjack” for some interesting (and I

Interesting. This seems to have to do with the Casinos doing away
from using a cut card and using a new machine which continuously
reshuffles used cards back into the “deck”. The traditional way for
casinos to deal blackjack is to shuffle several decks together, place
them into a shoe, insert a cut card towards the back of the deck, put
used cards at the back of the shoe, and reshuffle the whole deck when
the cut card gets near the front.

Of course, the real reason they do this is to minimize losses to card
counters. In the old-way players could gather information as cards
were removed from play, of course it’s quite a skill to remember the
information and be able to mentally keep track of the effect on the
odds. The one article I just read on the "cut card effect didn’t
mention that but only talked about the effect it has on players who
just use a “basic” strategy based on what’s in their current hand, and
what the dealer (and others at the same table) are showing currently.

Rick DeNatale

My blog on Ruby