Two Dice Monte Carlo Simulation

조회 수: 2 (최근 30일)
Maxine Killingsworth
Maxine Killingsworth 2019년 5월 1일
댓글: Maxine Killingsworth 2019년 5월 1일
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)

채택된 답변

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by