probability of getting three of a kind by drawing 5 cards

0 votes

so my goal is to try to simulate an actaul deck and draw five cards and check if there is a three of a kind. I have no problem making the deck and drawing five cards, the problem arises when i check for three of a kind

my code:

from random import shuffle, sample
from itertools import product
#generating  deck
suits = ["s","d","h","c"]
values = ["1","2","3","4","5","6","7","8","9","10","11","12","13"]
deck = list(product(values,suits))
sim = 100000

three_of_a_kind = 0
for i in range(sim):
  shuffle(deck)
  #generating hand
  hand = sample(deck,5)
  #checking for three of a kind
  if any(hand[0][0] == x[0] for x in hand):
    three1 += 1
  elif any(hand[1][0] == x[0] for x in hand):
    three2 += 1
  elif any(hand[2][0] == x[0] for x in hand):
    three3 += 1
  if three1 == 3 or three2 == 3 or three3 == 3:
    three_of_a_kind += 1
prob_three = three_of_a_kind/sim
print(prob_three)

edit: my deck only had 12 cards and I changed it to 13 but my question has not changed

Mar 23, 2022 in Machine Learning by Dev
• 6,000 points
736 views

1 answer to this question.

0 votes

from collections package import counter to count cards. The code is shown below;

from random import shuffle, sample
from itertools import product
from collections import Counter
#generating  deck
suit = ["s","d","h","c"]
cards = ["1","2","3","4","5","6","7","8","9","10","11","12","13"]
value = list(product(cards,suit))
val = 100000

three_of_a_kind = 0
for i in range(val):
    shuffle(values)
    
    #generating hand
    hand = sample(value,5)
    
    # Check for 3 of a kind by checking for 3 cards of same value
    if any(v==3 for k,v in Counter(card[0] for card in hand).items()):
           three_of_a_kind += 1
           
prob_three = three_of_a_kind/val
print(f'{prob_three:.4%}')
answered Mar 25, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer

Formula to calculate chance (probability) of a dice side based on its value

If I understand you correctly, you're looking ...READ MORE

answered Mar 17, 2022 in Machine Learning by Dev
• 6,000 points
774 views
0 votes
1 answer
0 votes
0 answers
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
0 votes
1 answer

Understanding the probability of a double-six if i roll two dice

The chance of not getting a double ...READ MORE

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
20,416 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP