I'm trying to find out how many times you have to throw the dice to get on file 5 100 times(board is played from 0 to 5). This is how I tried(I know the answer is 690 but I don't know what I'm doing wrong).
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count = count 1
print(randint(1,6))
add = add randint(1,6)
if add % 5 == 0 :
five = five 1
else: add = add randint(1,6)
print(count)
CodePudding user response:
This is the code I think you were trying to write. This does average about 600. Is it possible your "answer" came from Python 2? The random seed algorithm is quite likely different.
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count = 1
r = randint(0,5)
if r == 5:
five = 1
else:
add = r
print(count, add)
CodePudding user response:
You're adding a second dice throw every time you don't get on 5, this makes the probability distribution irregular (i.e. advancing by 7 will be more probable (1/6) than any other value, e.g. 1/9 for 5) so your result will not be the same as counting single throws.
BTW there is no fixed result for this, just a higher probability around a given number of throws. However, given that you seeded the random number generator with a constant, every run should give the same result. And it should be the right one if you don't double throw the dice.
Here is an example of the process that arrives at 690:
import random
random.seed(8)
fiveCount = 0
throwCount = 0
position = 0
while fiveCount < 100:
position = (position random.randint(1,6)) % 6
throwCount = 1
fiveCount = position == 5
print(throwCount) # 690
Other observations:
- Updating the position wraps around using modulo 6 (there are 6 positions from 0 to 5 inclusively)
- Your check of
add%5 == 0
does not reflect this. It should have beenadd%6 == 5
instead but it is always preferable to model the computation as close as possible to the real world process (so keep the position in the 0...5 range)