Borrar filtros
Borrar filtros

Two Dice Monte Carlo Simulation

3 visualizaciones (últimos 30 días)
Maxine Killingsworth
Maxine Killingsworth el 1 de Mayo de 2019
Comentada: Maxine Killingsworth el 1 de Mayo de 2019
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)

Respuesta aceptada

James Tursa
James Tursa el 1 de Mayo de 2019
Editada: James Tursa el 1 de Mayo de 2019
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 comentario
Maxine Killingsworth
Maxine Killingsworth el 1 de Mayo de 2019
Thank you! I was able to adjust the code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by