フィルターのクリア

[Ask Help] - Monte Carlo Coin Flip

4 ビュー (過去 30 日間)
Hilmi
Hilmi 2022 年 12 月 26 日
コメント済み: Image Analyst 2022 年 12 月 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;

回答 (2 件)

Image Analyst
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
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
probOf0 = 0.4900
probOf1 = E / N
probOf1 = 0.5100
There are literally only 3 characters you need to add to finish the script.
Image Analyst
Image Analyst 2022 年 12 月 30 日
@Hilmi please inform us of the current status.

サインインしてコメントする。


Torsten
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.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by