File Exchange

## Birthday probability solution

version 1.0.0.0 (1.52 KB) by samil

### samil (view profile)

Solution to the problem of finding the probability of at least two people having the same birthday

Updated 31 Jan 2012

Solution to birthday probability problem: If there are n people in a classroom, what is the probability that at least two of them have the same birthday?
General solution:
P = 1-365!/(365-n)!/365^n
If you try to solve this with large n (e.g. 30, for which the solution is 29%) with the factorial function like so:
P = 1-factorial(365)/factorial(365-30)/365^30
Matlab will output NaN because Matlab tries to compute very large values here.
The approach in this script is to start from n=1 and at each step use the previous step's result to avoid dealing with large numbers.

### Cite As

samil (2020). Birthday probability solution (https://www.mathworks.com/matlabcentral/fileexchange/34860-birthday-probability-solution), MATLAB Central File Exchange. Retrieved .

Karthik Kannappan

James Blanchard

### James Blanchard (view profile)

Here is a Monte Carlo solution. Increase the number of samples to improve the accuracy.

nump=30;
samples=10000;
birthd=ceil(365*rand(nump,samples));
count=0;
for j=1:samples
if numel(birthd(:,j))-numel(unique(birthd(:,j))) >0
count=count+1;
end
end
probab=1-count/samples

##### MATLAB Release Compatibility
Created with R2007a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux