Having completed the previous exercises you should be happy that the random numb
ID: 3772633 • Letter: H
Question
Having completed the previous exercises you should be happy that the random number generator in producing a uniform distribution of (pseudo)random numbers. Now we can continue to our first Monte Carlo simulation. Write a program that simulates the outcome of the flipping of an unbiased coin for n flips. Although it will slow the program down significantly, record the ratio of r = number of heats/number of flips for each coin flip. Make a plot of this ratio (as a function of iteration). Run your code for n = 10^1, 10^2, 10^5. Extend your program to consider x coins being flipped simultaneously. Compare your outcome for (n, x) = (10^3, 10) with (n, x) = (10, 1063). Print out your code and an exemplary plot for: (n, x) = (10^3, 100)Explanation / Answer
NUM_TRIALS = 150;
trials = 1:NUM_TRIALS;
heads = 0;
t = rand(NUM_TRIALS,1);
percent_h=zeros(size(t));
for i = trials
if (t(i) < 0.5)
heads = heads + 1;
end
percent_h(i) = heads/i;
end
plot(trials,percent_h)
xlabel('Trial Number')
ylabel('Percent Heads')
title ('Trial Number vs Percent Heads')
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.