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개)
Walter Roberson
2020년 4월 27일
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!