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!

Answers (1)

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;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 27 Apr 2020

Answered:

on 27 Apr 2020

Community Treasure Hunt

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

Start Hunting!