Use Monte Carlo simulation to approximate the probability of
Use Monte Carlo simulation to approximate the probability of two heads occurring when four coins are flipped.
Solution
The input of the M-file calculating this probability is the number of tosses and the output
is the probability of two heads occurring. Let 0 denote the tail and 1 denote the head.
function P=two heads(n)
c=0; (set counter to 0 initially)
for i=1:n
sum4 = round(rand(1))+round(rand(1))+round(rand(1))+round(rand(1))
(calculates the result of 4 tosses, for example if the outcome is 1 heads and 3 tails, the value of
sum4 is 1+0+0+0=1)
if sum4=2 c=c+1; (sum4=2 corresponds to exactly 2 heads in 4 tosses.
This is a positive outcome and we increase counter by 1 in this case)
end
end
P=c/n;
