[Ask Help] - Monte Carlo Coin Flip
4 views (last 30 days)
Show older comments
Anyone please could help me to solve this my question task?
Below is my question task...
Algorithm that can be used:
1. Generate the values 0 and 1 by 1000 (N=1000).
Here's how: generate random numbers using the LCG method, then operate modulo 2 on the random numbers that have been generated → the result must be a number 0 or 1.
2. Classification
▪ If n = 0, then M = M+1 → for example 0 is Face (M)
▪ If n = 1, then E = E+1 → for example 1 is Tail (E)
3. Calculate the probability M by means of M/N and probability E in the E/N way.
Below is my coding...
clear all;
clc;
Z0 = 3;
a = 4;
c = 7;
m1 = 1001;
m2 = 2;
max = 1000;
Z = [];
coin = [];
M = 0;
E = 0;
for i = 1:max
Z1 = (a*Z0+c);
Z1 = round(((Z1/m1) - fix(Z1/m1))*m1);
coin1 = round(((Z1/m2) - fix(Z1/m2))*m2);
Z = [Z;Z1];
coin = [coin; coin1];
Z0 = Z1;
end
plot(1:max,Z,'.')
I've a problem of my output using that coding and what should i do to show this M = 0; E = 0;
0 Comments
Answers (2)
Image Analyst
on 26 Dec 2022
What is the "LCG" method? I would just use randi but I don't know if it uses that LCG method internally or not.
N = 1000;
coinFlips = randi([0 1], 1, N);
M = 0;
E = 0;
for k = 1 : N
if coinFlips(k)
E =
else
end
end
% Compute probabilities:
probOf0 = M / N
probOf1 = E / N
I leave it to you to fill in the accumulation of M and E inside the if blocks
Anyway, they may want you to write your own random number generator.
4 Comments
Image Analyst
on 27 Dec 2022
OK, I guess not. So here it is inserted into the code:
N = 1000;
coinFlips = randi([0 1], 1, N);
M = 0;
E = 0;
for k = 1 : N
if coinFlips(k)
E = E + 1;
else
M = ;
end
end
% Compute probabilities:
probOf0 = M / N
probOf1 = E / N
There are literally only 3 characters you need to add to finish the script.
Torsten
on 26 Dec 2022
Add the lines
E = sum(coin);
M = max - E;
probOf0 = M / max
probOf1 = E / max
to your code.
But your way to generate the random numbers for "coin" seems to be biased since you don't approach 0.5 for both probablities.
0 Comments
See Also
Categories
Find more on Big Data Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!