-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdice.py
160 lines (121 loc) · 3.97 KB
/
dice.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy
from oct2py import octave
def getPValue(chiSquare):
if chiSquare <= 1.61:
return [0.9, 1]
if chiSquare <= 2.675:
return [0.75, 0.9]
if chiSquare <= 4.351:
return [0.50, 0.75]
if chiSquare <= 6.626:
return [0.25, 0.5]
if chiSquare <= 9.236:
return [0.1, 0.25]
if chiSquare <= 11.07:
return [0.05, 0.1]
if chiSquare <= 12.83:
return [0.025, 0.05]
if chiSquare <= 15.086:
return [0.01, 0.025]
return [0, 0.01]
class Die:
def __init__(self, rolls=None):
self.rolls = rolls if rolls else numpy.zeros(6).tolist()
self.numRolls = sum(self.rolls)
def addRolls(self, rolls):
self.rolls = numpy.add(self.rolls, rolls).tolist()
self.numRolls = sum(self.rolls)
def addRoll(self, roll):
self.rolls[roll-1] += 1
self.numRolls += 1
def getAverage(self):
total = 0
for i in xrange(0, 6):
total += self.rolls[i] * (i+1)
return round(float(total) / float(self.numRolls), 2)
def getChiSquare(self):
expected = float(self.numRolls) / 6.0
chiSquare = 0.0
for i in range(6):
chiSquare += pow(self.rolls[i] - expected, 2) / expected
return chiSquare
def testDie(self, colour):
# return
pValue = getPValue(self.getChiSquare())
if pValue[1] <= 0.05:
return (colour + " die seems to be rigged.\t" +
"( " + str(pValue[0]) + " > p > " + str(pValue[1]) + " )\n")
else:
return (colour + " die does not seem to be rigged.\t" +
"( " + str(pValue[0]) + " > p > " + str(pValue[1]) + " )\n")
class DiceSet:
def __init__(self):
self.redDie = Die()
self.yellowDie = Die()
self.rolls = numpy.zeros(11).tolist()
self.numRolls = 0
@classmethod
def loadFromDict(cls, data):
dice = DiceSet()
dice.redDie = Die(data['redDie'])
dice.yellowDie = Die(data['yellowDie'])
dice.rolls = data['rolls']
dice.numRolls = sum(dice.rolls)
return dice
def toDict(self):
return {'rolls': self.rolls, 'redDie': self.redDie.rolls, 'yellowDie': self.yellowDie.rolls}
def addFromDict(self, data):
self.redDie.addRolls(data['redDie'])
self.yellowDie.addRolls(data['yellowDie'])
self.rolls = numpy.add(self.rolls, data['rolls']).tolist()
self.numRolls = sum(self.rolls)
def addRoll(self, red, yellow):
self.redDie.addRoll(red)
self.yellowDie.addRoll(yellow)
self.rolls[red + yellow - 2] += 1
self.numRolls += 1
def getRolls(self):
red = self.redDie.rolls
yellow = self.yellowDie.rolls
combined = self.rolls
return (red, yellow, combined)
def getPercentages(self, numRolls=None):
if not numRolls:
numRolls = self.numRolls
red = [100*n/numRolls for n in self.redDie.rolls]
yellow = [100*n/numRolls for n in self.yellowDie.rolls]
combined = [100*n/numRolls for n in self.rolls]
return (red, yellow, combined)
def getAverages(self):
red = self.redDie.getAverage()
yellow = self.yellowDie.getAverage()
combined = red + yellow
return (red, yellow, combined)
def displaySeparately(self):
rTotal = 0
yTotal = 0
numRolls = self.numRolls if self.numRolls else 1 # Prevent dividing by 0
string = "RED:\t\t\tYELLOW:\n"
for i in range(6):
rPercent = round(float(self.redDie.rolls[i]) / float(numRolls) * 100, 2)
yPercent = round(float(self.yellowDie.rolls[i]) / float(numRolls) * 100, 2)
string += (str(i+1) + ": " + str(self.redDie.rolls[i]) +
"\t" + str(rPercent) + " %" +
"\t\t" + str(i+1) + ": " + str(self.yellowDie.rolls[i]) +
"\t" + str(yPercent) + " %\n")
rTotal += (i+1) * self.redDie.rolls[i]
yTotal += (i+1) * self.yellowDie.rolls[i]
rAverage = round(float(rTotal) / float(numRolls), 2)
yAverage = round(float(yTotal) / float(numRolls), 2)
string += "\n"
string += ("Average: " + str(rAverage) +
"\t\tAverage: " + str(yAverage) + "\n")
string += "\n"
return string
def graphResults(self, title):
octave.histogram(self.redDie.rolls, self.yellowDie.rolls, self.rolls, title)
def testDice(self):
string = self.displaySeparately()
string += self.redDie.testDie("Red")
string += self.yellowDie.testDie("Yellow")
return string