Suppose that 15 people are gathered in a room.
What are the odds that 2 people in the room share the same birthday?
Estimate the probability that two out of N people in a room share the same birthday, it is guaranteed that 2≤N≤30
The function sameBirthday takes one parameter, N which is the number of people gathered in a room. An empty vector peoplesBirthday is appended to add N number of random integers from 1 to 365 (with replacement), which is the number of days of the year in which an individual's birthday can fall on, assuming we are not including leap years.
The function then compares each value in the vector. If any two or more of the values match (i.e, a 'shared birthday'), the boolean value sharedBirthday returns true, else, it returns false.
The function probabilitySameBirthday takes two parameters, the number of repeated trials which will be conducted (the larger this number is the more accurate the estimated probability will be), and the number of people N in the room. Each loop calls upon the function sameBirthday. If sameBirthday has returned true, a counter variable increases by 1. The function then returns the cumulative probability which is calculated as the total number of trialsnumber of succcessful trials (counter).
I have estimated the probability of 2 or more out of 15 people sharing a birthday is ≈0.01042 or ≈1%.
import numpy as np import random def sameBirthday(N): sharedBirthday=False peoplesBirthday=[] for i in range(N): peoplesBirthday.append(np.random.randint(1, 365)) # print(peoplesBirthday) for i in range(N): if peoplesBirthday.count(i)>1: sharedBirthday=True return sharedBirthday def probabilitySameBirthday(trials, N): counter=0 for i in range(trials): if sameBirthday(N): counter+=1 probability=(counter/trials) return probability probabilitySameBirthday(100000, 15)