[Ask Help] - Monte Carlo Coin Flip
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;
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
Hilmi
on 26 Dec 2022
Image Analyst
on 26 Dec 2022
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own. You don't want to be thrown out for cheating, and we don't want you to get into trouble either.
It doesn't matter if you're new. You should be able to read the question where it tellls you very directly and explicitly that E = E + 1, and similar with M. I've given you a super strong hint as to where that line goes in my code. I think you should be able to know where to put it.
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.
Image Analyst
on 30 Dec 2022
@Hilmi please inform us of the current status.
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.
Categories
Find more on Logical 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!