-
Notifications
You must be signed in to change notification settings - Fork 0
/
w2l301_rollDice.py
41 lines (33 loc) · 879 Bytes
/
w2l301_rollDice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
def rollDie():
return random.choice([1,2,3,4,5,6])
def rollN(n):
result = ''
for i in range(n):
result = result + str(rollDie())
return result
print rollN(5)
def getTarget(goal):
numTries = 0
numRolls = len(goal)
while True:
numTries += 1
result = rollN(numRolls)
if result == goal:
return numTries
def runSim(goal, numTrials):
total = 0
for i in range(numTrials):
total += getTarget(goal)
print 'Average number of tries =', total/float(numTrials)
##runSim('11111', 100)
##runSim('54324', 100)
def atLeastOneOne(numRolls, numTrials):
numSuccess = 0
for i in range(numTrials):
rolls = rollN(numRolls)
if '1' in rolls:
numSuccess += 1
fracSuccess = numSuccess/float(numTrials)
print fracSuccess
atLeastOneOne(10, 1000)