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!

回答(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;

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by