by using python ; How many people do we need to have in a room to make it a favo
ID: 3586643 • Letter: B
Question
by using python ; How many people do we need to have in a room to make it a favorable bet (probability of success greater than 1/2) that at least two people in the room will have the same birthday? Ignore leap year and assume that the probability of birthday falling on any of 365 days is equal. To answer the above question, please write a Python function "same_bday_prob(i)" in the cell below. The function takes an integer greater than 1 as input and returns the probability that at least two people have the same birthday.
from math import factorial
from numpy import prod combin = lambda n, r : factorial(n)//factorial(r)//factorial(n-r)
def same_bday_prob(i):
Explanation / Answer
from __future__ import print_function
from math import factorial
import numpy as np
from decimal import Decimal
def same_bday_prob(i):
num1 = Decimal(factorial(365))
din1 = Decimal(factorial(365-i))
din2 = Decimal(pow(365, i))
not_same_bday = num1/(din1*din2)
return (Decimal(1)-not_same_bday) # not_same_bday + same_bday_prob = 1
#find how many users to have for favorable bet
users = range(2, 30)
user_samples = {} # dict with user count and its probabilities as key and values
for user in users:
user_prob = same_bday_prob(user)
if user_prob >= 0.5: # only considering cases with prob>=0.5
user_samples[user] = user_prob
#least peoples required
min_peoples = sorted(user_samples)[0] # sort dict according to its values
prob = user_samples[min_peoples]
print("min peoples", min_peoples, "with probability", prob)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.