Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 145
Image: ubuntu2004
Kernel: Python 3 (system-wide)

The Birthday Problem

Suppose that 1515 people are gathered in a room.

What are the odds that 22 people in the room share the same birthday?

Estimate the probability that two out of NN people in a room share the same birthday, it is guaranteed that 2N302\leq\N\leq30

The function sameBirthdaysameBirthday takes one parameter, NN which is the number of people gathered in a room. An empty vector peoplesBirthdaypeoplesBirthday is appended to add NN number of random integers from 1 to 3651\ 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 sharedBirthdaysharedBirthday returns truetrue, else, it returns falsefalse.

The function probabilitySameBirthdayprobabilitySameBirthday takes two parameters, the number of repeated trialstrials which will be conducted (the larger this number is the more accurate the estimated probability will be), and the number of people NN in the room. Each loop calls upon the function sameBirthdaysameBirthday. If sameBirthdaysameBirthday has returned truetrue, a counter variable increases by 11. The function then returns the cumulative probabilityprobability which is calculated as the number of succcessful trials (counter)total number of trials\frac{number\ of \ succcessful\ trials\ (counter)}{total\ number\ of\ trials}.

I have estimated the probability of 22 or more out of 1515 people sharing a birthday is 0.01042 or 1%\approx{0.01042}\ or\ \approx{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)
0.01053