1. Learn
  2. /
  3. Courses
  4. /
  5. Statistical Simulation in Python

Connected

Exercise

Two of a kind

Now let's use simulation to estimate probabilities. Suppose you've been invited to a game of poker at your friend's home. In this variation of the game, you are dealt five cards and the player with the better hand wins. You will use a simulation to estimate the probabilities of getting certain hands. Let's work on estimating the probability of getting at least two of a kind. Two of a kind is when you get two cards of different suites but having the same numeric value (e.g., 2 of hearts, 2 of spades, and 3 other cards).

By the end of this exercise, you will know how to use simulation to calculate probabilities for card games.

Instructions

100 XP
  • Deal the hand: In the for loop, shuffle deck_of_cards. We then select the first 5 cards as our hand.
  • Count numeric values: Utilize the get() method to construct the dictionary cards_in_hand which counts the occurrence of each numeric_value in hand.
  • Two of a kind? Check if the largest value in cards_in_hand is equal to or greater than 2 to see if we have at least two of a kind. If yes, we increment two_kind.