-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture11.py
More file actions
109 lines (90 loc) · 3.17 KB
/
lecture11.py
File metadata and controls
109 lines (90 loc) · 3.17 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
######## Lecture 11 ########
## Simulations ##
# 1. Define a function that runs a single trial
# of your simulation.
# 2. Call the function as many times as necessary,
# saving any needed values.
# 3. Do something with the data - average it out,
# make a graph, etc.
import random
def rollDie():
"""Returns a value from 1 to 6, inclusive,
representing the face of the die that was
rolled."""
return random.choice([1,2,3,4,5,6])
def runDieTrial(numTrials):
"""Runs a die-rolling simulation numTrials
times and returns the arithmetic mean of the
rolls.
numTrials: int representing the number of trials
returns: a float representing the mean value of
the roll"""
total = 0
for trial in range(numTrials):
total += rollDie()
# note that float isn't around the whole quantity
return total/float(numTrials)
print '5', runDieTrial(5)
print '50', runDieTrial(50)
print '500', runDieTrial(500)
print '5000', runDieTrial(5000)
print '50000', runDieTrial(50000)
def runDiceTrial(numDice, numTrials):
"""Runs a dice-rolling simulation numTrials
times with numDice dice and returns the
arithmetic mean of the rolls.
numTrials: int representing the number of trials
numDice: int representing the number of dice
returns: a float representing the mean value of
the roll"""
total = 0
for trial in range(numTrials):
for die in range(numDice):
total += rollDie()
# note that float isn't around the whole quantity
return total/float(numTrials)
print runDiceTrial(1, 5000)
print runDiceTrial(3, 5000)
class Die():
"""Represents a die."""
def __init__(self, numSides):
"""Creates a die object.
numSides: an int representing the number of
sides on the die."""
self.numSides = numSides
def roll(self):
"""Simulates a roll of the die by returning
an integer value.
returns: int between 1 and numSides, inclusive"""
# chooses a number from [1,2,3,4, ... numSides]
return random.choice(range(1, self.numSides + 1))
# Uncomment the following two methods to see the class
# instances print nicely
## def __str__(self):
## # Use return, not print!
## return "Die with " + str(self.numSides) + " sides"
##
## def __repr__(self):
## return self.__str__()
def runDiceObjectTrial(numDice, numTrials):
"""Runs a dice-rolling simulation numTrials
times with numDice 6-sided dice and returns the
arithmetic mean of the rolls.
numTrials: int representing the number of trials
numDice: int representing the number of dice
returns: a float representing the mean value of
the roll"""
total = 0
# Run the simulation numTrials times
for trial in range(numTrials):
# Create dice instances; store them in a list
diceList = []
for dieNum in range(numDice):
diceList.append(Die(6))
for die in diceList:
total += die.roll()
# note that float isn't around the whole quantity
print diceList
return total/float(numTrials)
print runDiceObjectTrial(1, 5000)
print runDiceObjectTrial(3, 5000)