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

2 views (last 30 days)
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.

Accepted Answer

KSSV
KSSV on 11 Sep 2020
Edited: KSSV on 11 Sep 2020
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

More Answers (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 11 Sep 2020
find(cumsum(randi(6,1,1000)==6)==25,1)
  3 Comments
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 11 Sep 2020
Edited: Asad (Mehrzad) Khoddam on 11 Sep 2020
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 on 12 Sep 2020
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!