Answer on Question #45549,
Programming, Mat LAB | Mathematica | MathCAD | Maple
Problem.
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 40
Solution.
Code (MATLAB)
function probability()
clc();
% Input
n = input('The number of people: ');
m = 1000;
simSuc = 0;
% Simulation loop
for i = 1:1:m
simGrp = randi(365, 1, n);
if length(unique(simGrp)) == n
simSuc = simSuc + 1;
end
end
% Output
fprintf('The probability that two or more of %d people\nwill have the same birthday equals %f\n', n, simSuc/m);
endResult
The number of people: 25
The probability that two or more of 25 people will have the same birthday equals 0.580000
>>