SHOW:
|
|
- or go back to the newest paste.
| 1 | ||
| 2 | import random as random | |
| 3 | from math import floor | |
| 4 | ||
| 5 | def myfunc(n): | |
| 6 | '''output estimated dice probabilities''' | |
| 7 | probabilities = [0]*6 | |
| 8 | - | for i in range(n): |
| 8 | + | def update(): |
| 9 | '''Toss a dice in the air, update the estimated probability''' | |
| 10 | roll_i = random.uniform(0,1) | |
| 11 | idx = int(floor(roll_i*6)) # Map to 0, 1, 2, 3, 4, 5 with equal probability | |
| 12 | probabilities[idx]+=1.0/n | |
| 13 | ||
| 14 | - | prob = myfunc(10000) |
| 14 | + | [update() for i in range(n)] |
| 15 | return probabilities | |
| 16 | ||
| 17 | ||
| 18 | prob = myfunc(100000) | |
| 19 | print(prob) |