Roll a 6-sided die until you have rolled a total of 25 6's.

조회 수: 2 (최근 30일)
Colton Baker
Colton Baker 2020년 9월 11일
댓글: Walter Roberson 2020년 9월 12일
Hello,
I am trying to wrtie a script to roll randi(6) and loop until it has reached 25 sixes and then count how many rolls it took. Help please.

채택된 답변

KSSV
KSSV 2020년 9월 11일
편집: KSSV 2020년 9월 11일
count = 0 ;
iwant = zeros([],1) ;
i = 0 ;
while count ~= 25
i = i+1 ;
N = randi(6) ;
iwant(i) = N ;
if N == 6
count = count+1 ;
end
end

추가 답변 (1개)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 11일
find(cumsum(randi(6,1,1000)==6)==25,1)
  댓글 수: 3
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 11일
편집: Asad (Mehrzad) Khoddam 2020년 9월 11일
Yes, you are right. Simply if it happens, you can try again
The probability is 1.536e-48 (it happens once in 6.5e47 tries)
Walter Roberson
Walter Roberson 2020년 9월 12일
You wouldn't quite just try again... you would have to subtract off the number of 6's that you got up to that point, and you would have to add the number of attempts you already made to the index.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by