Assistance with Dice Simulation script

The objective is to create a simulation to count the number of rolls it takes to roll two dice until both show the number 6. The script is supposed to count the number of rolls it takes for a total of 10000 trials.
NumberTrials=10000;
rollcount=0;
Counter=[];
success=0;
for i=1:NumberTrials
success=0;
rollcount=0;
while success==0
Roll1=randi(6,1);
Roll2=randi(6,1);
rollcount=rollcount+1
if Roll1==6 && Roll2==6
success=1
end
end
end
This is the code i have. The problem i am having is that the script continues to run past 10000 trials and I dont know how to store the number of rolls it takes until a success into a vector. Any help would be appreciated!

Respuestas (1)

Walter Roberson
Walter Roberson el 27 de Abr. de 2020
After the while loop:
rollcounts(i) = rollcount;
Also,
rollcount=rollcount+1
I recommend changing that to
rollcount=rollcount+1;
and
success=1
to
success=1;

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 27 de Abr. de 2020

Respondida:

el 27 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by