Blackjack, what are the chances?

To start the game of Blackjack, players are dealt two cards at random from a shuffled deck.

You write the following code to simulate the act of dealing an initial hand. To test the code, you deal a hand 1 0 6 10^6 times and record the number of times the player makes Blackjack on their first two cards. If the code is written correctly, what do you expect to find for f ^ blackjack \hat{f}_\text{blackjack} , the fraction of initial hands that are Blackjack?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from random import shuffle

# define the card ranks, and suits
ranks = [_ for _ in range(2, 11)] + ['JACK', 'QUEEN', 'KING', 'ACE']
suits = ['SPADE', 'HEART ', 'DIAMOND', 'CLUB']

def get_deck():
    """Return a new deck of cards."""
    return [[rank, suit] for rank in ranks for suit in suits]

# get a deck of cards, and randomly shuffle it
deck = get_deck()
shuffle(deck)

# issue the player and dealer their first two cards
player_hand = [deck.pop(), deck.pop()]

Assumptions and Details

  • A two card hand is said to be "blackjack" if it consists of an Ace and any card worth 10 (i.e. a ten, or a face card).


The answer is 0.0482655.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Pranshu Gaba
May 2, 2015

Warning: I have just used brute-force. It is very inefficient and slow, but it was the best I could do. Also, the answer is not precise, and keeps on changing every time the code is run. The precise answer can be obtained by running this code several times and taking an average.

I would like to see a better solution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from random import shuffle

# define the card ranks, and suits
ranks = [_ for _ in range(2, 11)] + ['JACK', 'QUEEN', 'KING', 'ACE']
suits = ['SPADE', 'HEART ', 'DIAMOND', 'CLUB']

def get_deck():
    """Return a new deck of cards."""
    return [[rank, suit] for rank in ranks for suit in suits]

# get a deck of cards, and randomly shuffle it
deck = get_deck()
shuffle(deck)

# issue the player and dealer their first two cards
player_hand = [deck.pop(), deck.pop()]


def isBJ(player_hand):
    if player_hand[0][0] == "ACE":
        if player_hand[1][0] == 10 or player_hand[1][0] == "JACK" or player_hand[1][0] == "QUEEN" or player_hand[1][0] == "KING":
            return True
        else:
            return False
    elif  player_hand[0][0] == 10 or player_hand[0][0] == "JACK" or player_hand[0][0] == "QUEEN" or player_hand[0][0] == "KING":
        if player_hand[1][0] == "ACE":
            return True
        else:
            return False
    else:
        return False

count = 0 # this is total number of times hand is dealt
count_True = 0 # number of hands that are Blackjack
count_False = 0 # number of hands that are not Blackjack

while count < 10**6:
    print player_hand
    print isBJ(player_hand)
    if isBJ(player_hand):
        count_True += 1
    else:
        count_False += 1
    deck = get_deck()
    shuffle(deck)
    player_hand = [deck.pop(), deck.pop()]
    count += 1

print count
print count_True
print count_False

To get f ^ blackjack \hat{f} _{\text{blackjack}} , we divide count_True by count .

When I ran this code, I got count_True = 48215 = 48215 , so the fraction f ^ blackjack 0.0482 \hat{f} _{\text{blackjack}} \approx 0.0482

@Pranshu Gaba

We just need an ace and a card worth 10. There are 4 aces, and 4 × 4 = 16 4\times 4 = 16 cards worth 10. The probability is 2 × 4 52 × 16 51 . 2 \times \frac{4}{52} \times \frac{16}{51}.

Eli Ross Staff - 5 years, 4 months ago

Log in to reply

I can't believe it... The solution is so simple. The wording of the question is such that I did not even consider using a mathematical approach. I only focussed on a computer science solution. Thank you for bringing this to my attention. :) From now onwards, I will try various approaches and think in different ways to arrive at the solution.

Pranshu Gaba - 5 years, 4 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...