Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
monte-carlo solution in Uncategorized category for Expected Dice by ciel
import random
def expected(n, s, t, b):
_t=200000
_s=0
for _i in range(_t):
turn=0
cur=0
while cur!=t:
cur=(cur+sum(random.randint(1,s) for z in range(n)))%len(b)
cur=(cur+b[cur]+len(b))%len(b)
turn+=1
_s+=turn
print(_s*1.0/_t)
return _s*1.0/_t
if __name__ == '__main__':
def almost_equal(checked, correct, significant_digits=1):
precision = 0.1 ** significant_digits
return correct - precision < checked < correct + precision
assert(almost_equal(expected(1, 4, 3, [0, 0, 0, 0]), 4.0))
assert(almost_equal(expected(1, 4, 1, [0, 0, 0, 0]), 4.0))
assert(almost_equal(expected(1, 4, 3, [0, 2, 1, 0]), 1.3))
assert(almost_equal(expected(1, 4, 3, [0, -1, -2, 0]), 4.0))
assert(almost_equal(expected(1, 6, 1, [0] * 10), 8.6))
assert(almost_equal(expected(2, 6, 1, [0] * 10), 10.2))
June 18, 2014