# Answer to Question #45329 in MatLAB | Mathematica | MathCAD | Maple for Akshay Joshi

Question #45329

The Birthday Problem: The birthday problem is stated as follows:

If there is a group of n people in a room, what is the probability that two or more of them having same

birthday? It is possible to determine answer to this question by simulation. (Hint: You can generate

random dates, n times and determine the fraction of people who born in a given day). Write a function

that determines the answer to this question by simulation. The program you write can take n as the

input and prints out the probability that two or more of n people will have the same birthday for

n=2,3,4…. 40

If there is a group of n people in a room, what is the probability that two or more of them having same

birthday? It is possible to determine answer to this question by simulation. (Hint: You can generate

random dates, n times and determine the fraction of people who born in a given day). Write a function

that determines the answer to this question by simulation. The program you write can take n as the

input and prints out the probability that two or more of n people will have the same birthday for

n=2,3,4…. 40

Expert's answer

`close all; clear all; clc;`

date_min = datenum([1960 1 1]); % start date

date_max = floor(now); % current date

N = 40;

P = zeros(1,N-1);

for n=2:N

date_r = randi([date_min date_max], 1, n); % random day

date_r_d = datevec(date_r);

d = date_r_d(:,3);

m = date_r_d(:,2);

c = 0;

for i = 1:n

for j = 1:n

if m(i) == m(j) && d(i) == d(j) && i ~= j

c = c + 1;

end

end

end

P(n-1) = c/n;

end

P

## Comments

## Leave a comment