필터 지우기
필터 지우기

[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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by