% Edit the line below and observe the effects on the results
n_experiments = 100000;
% Initialize an empty vector to store the results
results = [];
% Main loop over experiments
for i=1:n_experiments
% Generate ten random numbers in the range 0 to 1, count how
% many of them exceed 0.5 - this is equivalent to tossing a head
n_heads = 0;
for j=1:10
if rand() > 0.5
n_heads = n_heads + 1;
end
end
% Record how many heads there were in this experiment
results = [results n_heads];
end
% Plot a historgram of the number of heads
hist(results, [0:10]);
grid on;
% Display frequencies relating to parts (i) and (ii) of the question
disp('Proportion of experiments in which there were five heads:');
disp(sum(results == 5)/n_experiments);
disp('Proportion of experiments in which there were seven heads:');
disp(sum(results == 7)/n_experiments);
disp('Proportion of experiments in which there were more than seven heads:');
disp(sum(results > 7)/n_experiments);