INTRODUCTION

A standard deck of cards contains 4 suits (spades, diamonds of hearts and clubs) and 13 face cards (2 – 10, Jack, Queen, King and Ace). This means that there are 52 cards in such a deck.

Card games are usually played with 2, 3 or 4 people. Therefore, you can simulate the random allocation of cards and observe the results obtained.

You can analyze the results as they are displayed, but you can also try to automate this process:

FUN FACT

The probability of drawing a specific card from a full deck is152.

However, if we want to calculate the probability of getting a set of cards, then we have to use more complicated calculations, for example, we have to use combinations of:

$C_{n}^{k}=
\begin{pmatrix}
n\\
k
\end{pmatrix}=\frac{n!}{(n-k)! \cdot k!} $

EXAMPLE:

We will calculate the probability of getting three queens:

  • we pick three cards from the deck, so we have $C_{52}^{3}=
    \begin{pmatrix}
    52\\
    3
    \end{pmatrix}=\frac{52!}{(52-3)! \cdot 3!}=\frac{52\cdot 51\cdot 50\cdot 49!}{49!\cdot 3\cdot 2\cdot 1}=\frac{52\cdot 51\cdot 50}{3\cdot 2\cdot 1} =22100$ possibilities
  • there are four queens in the deck, but we want three, so we have: $C_{4}^{3}=
    \begin{pmatrix}
    4\\
    3
    \end{pmatrix}=\frac{4!}{(4-3)! \cdot 3!}=4$ possibilities
  • the probability of getting three queens from the whole deck is then: $P=\frac{4}{22100}=\frac{1}{5525}$ 

PYTHON CODE

import random

# Create a deck of cards
cards = []
for suit in ['\u2665', '\u2666', '\u2663', '\u2660']:
  for rank in range(2, 11):
    cards.append((suit, rank))
  cards.append((suit, 'J'))
  cards.append((suit, 'Q'))
  cards.append((suit, 'K'))
  cards.append((suit, 'A'))

# Shuffle the deck
random.shuffle(cards)

# Ask the user how many players there are
number_of_players = int(input("How many players? "))

# Check if the number of players is valid
while number_of_players < 2 or number_of_players > 4:
  print("The number of players must be between 2 and 4.")
  number_of_players = int(input("How many players? "))

# Ask the user if all the cards should be dealt
deal_all_cards = input("Do you want to deal all the cards? (y/n) ")

# If not all the cards should be dealt, get the number of cards per player from the user
if deal_all_cards == "n":
  number_of_cards_per_player = int(input("How many cards per player? "))
else:
  number_of_cards_per_player = int(52 / number_of_players)

# Deal the cards to the players
player_cards = []
for player in range(number_of_players):
  player_cards.append([])
  for card in range(number_of_cards_per_player):
    card_tuple = cards.pop()
    card_value = card_tuple[1]
    card_suit = card_tuple[0]
    player_cards[player].append(f'{card_value}{card_suit}')

# Print the cards of each player
for player in range(number_of_players):
  print("Player {}:   ,{}".format(player + 1, ','.join(player_cards[player])))

⬆️⬆️⬆️ See in Google Colaboratory


HOW THE CODE WORKS?

  • The obtained result can be imported into a spreadsheet and further analyze which cards were drawn:
  • Copy the result by selecting it with the mouse and pressing Ctrl+C
    • Player 1:   ,5♣, 5♦, 2â™ , A♣, 6♣, 8♦, K♣, 10♦, J♦, Aâ™ , 4♥, 3♥, A♥, 9♣, K♦, 3â™ , Q♥
    • Player 2:   ,9♦, Q♦, J♣, 10♥, 10♣, 4♣, 7♥, 4â™ , A♦, 3♣, Kâ™ , 8â™ , K♥, 4♦, 6♦, 2♥, 8♣
    • Player 3:   ,8♥, Qâ™ , 7♣, 10â™ , 6♥, 5♥, 9â™ , Q♣, 2♦, 9♥, Jâ™ , 5â™ , 3♦, 7â™ , J♥, 6â™ , 2♣
  • Open Google Spreadsheet, you can use sheets.new address
  • Paste the data saved in the clipboard
    • in Edit > Paste Special > Values ​​Only
    • or by pressing Ctrl + Shift + V

To separate tabs into separate cells:

  • in cell B1 adjacent to the first result A1, enter the formula:=split(A1;„,”)
  • then grab the lower right corner of cell B1 in the shape of a blue circle with the mouse and drag down