MATLAB Answers

Two Dice Monte Carlo Simulation

33 ビュー (過去 30 日間)
On a roll of two dice, a total of seven occurs with probability 1=6. In 100 rolls of the dice, what is the probability that five consecutive rolls of seven will occur? Model using Monte Carlo simulation.
..................................................
p=1/6;
N=100;
sumseven=zeros(N,1);
for m=1:N
l=0;
for k=1:5
x=rand;
if x<p
l=l+1;
else
l=0;
end
if l==5
sumseven(m)=1;
break;
end
end
end
p_sumseven=sum(sumseven(:))/N;
fprintf('Probability of a 5 consecutive rolls of 7: p=%6.5f\n',p_sumseven);
........................................
not sure about (if x < p)

  0 件のコメント

サインイン to comment.

採用された回答

James Tursa
James Tursa 2019 年 5 月 1 日
編集済み: James Tursa 2019 年 5 月 1 日
You don't have your for-loops set up properly. You are trying to find the probability that five consecutive 7's occur in 100 rolls of two dice. So, one trial consists of 100 rolls of two dice. Your Monte Carlo simulation should have an outer loop running the number of trials, and inside this loop is one trial consisting of 100 rolls of the dice where you look for five consecutive 7's. An outline something like this based on your nomenclature:
p=1/6; % probability of getting a 7 with two dice
ntrials = 100000; % pick some large number of trials
N=100; % the number of dice rolls in one trial
sumseven=zeros(ntrials,1); % changed N to ntrials
for m=1:ntrials % changed N to ntrials
% inside this loop is the code for one trial, which is 100 rolls of the dice
l=0;
for k=1:N % changed 5 to N
% your code here for rolling two dice and seeing if you have 5 consecutive 7's
end
end
p_sumseven=sum(sumseven(:))/ntrials; % changed N to ntrials
% etc.

  1 件のコメント

Maxine Killingsworth
Maxine Killingsworth 2019 年 5 月 1 日
Thank you! I was able to adjust the code.

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。


Translated by