Having completed the previous exercises you should be happy that the random numb
ID: 3772634 • 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
Here is the code...
Online compiler busy... so i am unable to attach output... any way here is the code
%number of toss times declared
tossTimes = [10,100,1000,10000,100000]
%array to hold the ratios
ratio = [0,0,0,0,0]
counter = 0
%calculating each ratio for each toss times
for NUM_TRIALS = tossTimes
trials = 1:NUM_TRIALS;
heads = 0;
tails = 0;
t = rand(NUM_TRIALS,1);
for i = trials
%checking for heads
if (t(i) < 0.5)
heads = heads + 1;
else
%incrementing tails counter
tails = tails+1;
end
%funding ratio and storing into array
ratio(counter) = heads/tails;
counter = counter+1;
end
end
%plotting final results
plot(tossTimes,ratio)
xlabel('Trial Number')
ylabel('Heads Tails Ratio')
title ('Trial Number vs Heads Tails Ratio')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.