Skip to content

Commit 230a0fd

Browse files
committed
Commiting coinToss and updated readme
1 parent bb00b7e commit 230a0fd

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ connectFour originated as a project in my CS 5 (introduction to computer science
2121
class in which we designed a basic AI to play against other AIs or against humans
2222
(or to play two humans against one another). I made a few improvements to the
2323
graphics and user input over the summer, so here it is.
24+
25+
====== Update ======
26+
27+
On November 30, 2014 I added to the file betrayal.py, which is code designed to model
28+
the tabletop game created by JT Booth on which he, John Phillpot and I have been working.
29+
This is only one side of the game which we are developing, a tool for the game master to
30+
use to keep track of his or her players.

coinToss.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 28 20:34:26 2014
4+
5+
@author: Michael
6+
"""
7+
8+
import math
9+
from matplotlib import pyplot
10+
import random
11+
12+
def tossCoin():
13+
return random.choice(["H", "T"])
14+
15+
def waitForString(string):
16+
"""returns the number of flips needed until
17+
we get a desired sequence"""
18+
n = len(string)
19+
lastN = []
20+
trials = 0
21+
while len(lastN) < len(string):
22+
lastN += [tossCoin()]
23+
trials += 1
24+
while not same(string, lastN):
25+
lastN = lastN[1:] + [tossCoin()]
26+
trials += 1
27+
return trials
28+
29+
def same(string, L):
30+
for x in range(len(L)):
31+
if string[x] != L[x]:
32+
return False
33+
return True
34+
35+
def getTstats(string1, string2, trials=1000):
36+
"""gets a t-score for a pooled test comparing
37+
the number of flips needed for two different strings"""
38+
r1 = []
39+
r2 = []
40+
for trial in range(trials):
41+
r1.append(waitForString(string1))
42+
r2.append(waitForString(string2))
43+
avg1 = sum(r1)/float(trials)
44+
avg2 = sum(r2)/float(trials)
45+
var1 = 1/float(trials-1)*sum([(x - avg1)**2 for x in r1])
46+
var2 = 1/float(trials-1)*sum([(x - avg2)**2 for x in r2])
47+
S_p = math.sqrt((trials-1)*(var1 + var2)/float(2*trials - 2))
48+
df = 2*trials - 2
49+
print "Difference in variances is: ", (var2 - var1)/((var1 + var2)/2)
50+
T = (avg1 - avg2)/(S_p*math.sqrt(2/float(trials)))
51+
return T, df
52+
53+
#the main verifies that not all strings
54+
#of the same length take the same number
55+
#of flips on average
56+
trials = 500
57+
r1 = []
58+
r2 = []
59+
for trial in range(trials):
60+
r1.append(waitForString(10*"H"))
61+
r2.append(waitForString(5*"HT"))
62+
pyplot.hist(r1)
63+
pyplot.hist(r2)

0 commit comments

Comments
 (0)