Answer on Question #45296,
Programming, Mat LAB | Mathematica | MathCAD | Maple
Problem.
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 f
unction
that determines the answer to this question by simulation. The
program
you wr
ite 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
Solution.
Code (MATLAB)
function probability()
clc();
% Input
n = input('The number of people: ');
for i = 1:1:4
% The number of simulations
m = 10^i;
% The number of successful simulations
% (when there is two or more people with same birthday date)
simSuc = 0;
% Simulation loop
for j = 1:1:m
simGrp = randi(365, 1, n);
if length(unique(simGrp)) == n
simSuc = simSuc + 1;
end
end
% Output
fprintf('The probability equals %f (%d simulations)\n', simSuc/m, m);
end
endResult
Command Window
→ □ × ×
The number of people: 30
The probability equals 0.600000 (10 simulations)
The probability equals 0.620000 (100 simulations)
The probability equals 0.719000 (1000 simulations)
The probability equals 0.701900 (10000 simulations)http://www.AssignmentExpert.com/
Comments