# Estimate Probabilities of Card Games

## A practical example of how you can calculate Card Probabilities with Monte Carlo Simulation and Numerically Photo by Amanda Jones on Unsplash

We are going to show how we can estimate card probabilities by applying Monte Carlo Simulation and how we can solve them numerically in Python. The first thing that we need to do is to create a deck of 52 cards.

How to Generate a Deck of Cards

`import itertools, random# make a deck of cardsdeck = list(itertools.product(['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],['Spade','Heart','Diamond','Club']))deck`

And we get:

`[('A', 'Spade'), ('A', 'Heart'), ('A', 'Diamond'), ('A', 'Club'), ('2', 'Spade'), ('2', 'Heart'), ('2', 'Diamond'), ('2', 'Club'), ('3', 'Spade'), ('3', 'Heart'), ('3', 'Diamond'), ('3', 'Club'), ('4', 'Spade'), ('4', 'Heart'), ('4', 'Diamond'), ('4', 'Club'), ('5', 'Spade'), ('5', 'Heart'),...`

How to Shuffle the Deck

`# shuffle the cardsrandom.shuffle(deck)deck[0:10]`

And we get:

`[('6', 'Club'), ('8', 'Spade'), ('J', 'Heart'), ('10', 'Heart'), ('Q', 'Spade'), ('7', 'Diamond'), ('K', 'Diamond'), ('J', 'Club'), ('J', 'Diamond'), ('A', 'Club')]`

How to Sort the Deck

For some probabilities where the order does not matter, a good trick is to sort the cards. The following commands can be helpful.

`# sort the decksorted(deck)# sort by facesorted(deck, key = lambda x: x)# sort by suitsorted(deck, key = lambda x: x)`

How to Remove Cards from the Deck

Depending on the Games and the problem that we need to solve, sometimes there is a need to remove from the Deck the cards which have already been served. The commands that we can use are the following:

`# assume that card is a tuple like ('J', 'Diamond')deck.remove(card)# or we can use the pop command where we remove by the indexdeck.pop(0)# or for the last carddeck.pop()`

# Part 1: Estimate Card Probabilities with Monte Carlo Simulation

Question 1: What is the probability that when two cards are drawn from a deck of cards without a replacement that both of them will be Ace?

Let’s say that we were not familiar with formulas or that the problem was more complicated. We could find an approximate probability with a Monte Carlo Simulation (10M Simulations)

`N = 10000000double_aces = 0for hands in range(N):    # shuffle the cards    random.shuffle(deck)    aces = [d for d in deck[0:2]].count('A')    if aces == 2:        double_aces+=1prob = double_aces/Nprob`

And we get `0.0045214` where the actual probability is 0.0045

Question 2: What is the probability of two Aces in 5 card hand without replacement.

FYI: In case you want to solve it explicitly by applying mathematical formulas:

`from scipy.stats import hypergeom# k is the number of required aces# M is the total number of the cards in a deck# n how many aces are in the deck# N how many aces cards we will gethypergeom.pmf(k=2,M=52, n=4, N=5)`

And we get `0.03992981808107859`. Let’s try to solve it by applying simulation:

`N = 10000000double_aces = 0for hands in range(N):    # shuffle the cards    random.shuffle(deck)    aces = [d for d in deck[0:5]].count('A')    if aces == 2:        # print(deck[0:2])        double_aces+=1prob = double_aces/Nprob`

And we get `0.0398805.` Again, quite close to the actual probability

Question 3: What is the probability of being dealt a flush (5 cards of all the same suit) from the first 5 cards in a deck?

If you would like to see the mathematical solution of this question you can visit PredictiveHacks. Let’s solve it again by applying a Monte Carlo Simulation.

`N = 10000000flushes = 0for hands in range(N):    # shuffle the cards    random.shuffle(deck)    flush = [d for d in deck[0:5]]    if len(set(flush))== 1:        flushes+=1prob = flushes/Nprob`

And we get `0.0019823` which is quite close to the actual probability which is 0.00198.

Question 4: What is the probability of being dealt a royal flush from the first 5 cards in a deck?

The actual probability of this case is around 0.00000154. Let’s see if the Monte Carlo works with such small numbers.

`# royal flushN = 10000000royal_flushes = 0for hands in range(N):    # shuffle the cards    random.shuffle(deck)    flush = [d for d in deck[0:5]]    face = [d for d in deck[0:5]]    if len(set(flush))== 1 and sorted(['A','J', 'Q', 'K', '10'])==sorted(face):    royal_flushes+=1prob = royal_flushes/Nprob`

And we get `1.5e-06` which is a very good estimation.

# Part 2: Calculate Exact Card Probabilities Numerically

Above, we showed how we can calculate the Card Probabilities explicitly by applying mathematical formulas and how we can estimate them by applying Monte Carlo Simulation. Now, we will show how we can get the exact probability using Python. This is not always applicable but let’s try to solve the questions of Part 1.

The logic here is to generate all the possible combinations and then to calculate the ratio. Let’s see how we can get all the possible 5-hand cards of a 52-card deck.

`# importing modulesimport itertoolsfrom itertools import combinations# make a deck of cardsdeck = list(itertools.product(['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], ['Spade','Heart','Diamond','Club']))# get the (52 5) combinationsall_possible_by_5_combinations = list(combinations(deck,5))len(all_possible_by_5_combinations)`

And we get `2598960` as expected.

Question 1: What is the probability that when two cards are drawn from a deck of cards without a replacement that both of them will be Ace?

`# importing modulesimport itertoolsfrom itertools import combinations# make a deck of cardsdeck = list(itertools.product(['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], ['Spade','Heart','Diamond','Club']))# get the (52 2) combinationsall_possible_by_2_combinations = list(combinations(deck,2))Aces = 0for card in all_possible_by_2_combinations:    if [d for d in card].count('A') == 2:        Aces+=1prob = Aces / len(all_possible_by_2_combinations)prob`

And we get `0.004524886877828055.`

Question 2: What is the probability of two Aces in 5 card hand without replacement.

`# get the (52 5) combinationsall_possible_by_5_combinations = list(combinations(deck,5))Aces = 0for card in all_possible_by_5_combinations:    if [d for d in card].count('A') == 2:        Aces+=1prob = Aces / len(all_possible_by_5_combinations)prob`

And we get `0.03992981808107859`

Question 3: What is the probability of being dealt a flush (5 cards of all the same suit) from the first 5 cards in a deck?

`# get the (52 5) combinationsall_possible_by_5_combinations = list(combinations(deck,5))flushes  = 0for card in all_possible_by_5_combinations:    flush = [d for d in card]    if len(set(flush))== 1:        flushes+=1prob = flushes / len(all_possible_by_5_combinations)prob`

And we get `0.0019807923169267707`

Question 4: What is the probability of being dealt a royal flush from the first 5 cards in a deck?

`# get the (52 5) combinationsall_possible_by_5_combinations = list(combinations(deck,5))royal_flushes   = 0for card in all_possible_by_5_combinations:    flush = [d for d in card]    face = [d for d in card]    if len(set(flush))== 1 and sorted(['A','J', 'Q', 'K', '10'])==sorted(face):        royal_flushes +=1prob = royal_flushes  / len(all_possible_by_5_combinations)prob`

And we get `1.5390771693292702e-06`

# Discussion

Computing Power enables us to estimate and even calculate complicated probabilities without being necessary to be experts in Statistics and Probabilities. In this post, we provided some relatively simple examples. The same logic can be extended to more advanced and complicated problems in many fields, like simulating card games, lotteries, gambling games etc.

Originally published at https://predictivehacks.com.

## The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Written by

## George Pipis

Data Scientist @ Persado | Co-founder of the Data Science blog: https://predictivehacks.com/

## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Written by

## George Pipis

Data Scientist @ Persado | Co-founder of the Data Science blog: https://predictivehacks.com/ ## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

## More From Medium

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium