% LMex020509_4th.m
% Example 2.5.9 Application of a Monte Carlo simlation  p 63-64 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu, revised 1/15/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Probability of survival of 3 independent individuals
PA1=.7;PA2=.9;PA3=0.3;
% The probability of exactly one death is
P=PA1*PA2*(1-PA3)+PA1*(1-PA2)*PA3+(1-PA1)*PA2*PA3;
deaths=0;
trials=1e6;
exactlyone=0;
for i=1:trials
    death=0;
    if rand>PA1;death=death+1;end
    if rand>PA2;death=death+1;end
    if rand>PA3;death=death+1;end
    if death==1;
        exactlyone=exactlyone+1;
    end
end
Pexactlyone=exactlyone/trials;
fprintf('The exact P is %5.3f and Monte Carlo simulated P is %5.3f.\n',...
    P,Pexactlyone)