# Not sure why my code results in a wrong figure. I am trying to simulate 2 dice rolls and counting how many times it takes to roll 2 6s out 10000 trials.

조회 수: 2(최근 30일)
Enkhdul Batkhuyag 2020년 4월 28일
댓글: Mrutyunjaya Hiremath 2020년 4월 28일
num_trial=10000;
num_until_success=1;
counter=[];
success_count=0;
for i=1:num_trial
success_count=0;
num_until_success=0;
while success_count==0
roll_1=randi(6,1);
roll_2=randi(6,1);
num_until_success=num_until_success + 1;
if roll_1==6 && roll_2==6
success_count=1;
end
end
end
histogram(success_count,10);
xlabel('Number of consecutive rolls until two 6s')
ylabel('Number of occurences')
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Enkhdul Batkhuyag 2020년 4월 28일
This the figure I get from my code but the Y-axis is fixed to one and doesn't track the number of occurences per number of rolls until 2 6s 댓글을 달려면 로그인하십시오.

### 채택된 답변

Mrutyunjaya Hiremath 2020년 4월 28일
Hello Enkhdul Batkhuyag,
Try this one -
num_trial=10000;
num_until_success=0;
for i=1:num_trial,i
roll_1=randi(6,1);roll_1
roll_2=randi(6,1);roll_2
if roll_1==6 && roll_2==6
num_until_success=num_until_success + 1;
end
end
disp(num_until_success)
##### 댓글 수: 8표시숨기기 이전 댓글 수: 7
Mrutyunjaya Hiremath 2020년 4월 28일
@ James Tursa
Agreed. :)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!