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 1to365 (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 totalnumberoftrialsnumberofsucccessfultrials(counter).
I have estimated the probability of 2 or more out of 15 people sharing a birthday is ≈0.01042or≈1%.