[Ask Help] - Monte Carlo Coin Flip
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Image Analyst
2022-12-26
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 个评论
Image Analyst
2022-12-27
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
2022-12-26
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!