/gist:12bdcc2806745bb75658 Secret
Created
October 20, 2011 23:01
Star
You must be signed in to star a gist
FairFlip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import random | |
import time | |
def analyzeCoin(coin, numSamples=50000): | |
numTails = 0 | |
numHeads = 0 | |
for x in xrange(numSamples): | |
result = coin() | |
if result == 1: numHeads += 1 | |
elif result == 0: numTails += 1 | |
pH = int((float(numHeads)/numSamples)*100) | |
pT = int((float(numTails)/numSamples)*100) | |
print "After %d flips, this coin landed heads %d%% of the time and tails %d%% of the time" % (numSamples, pH, pT) | |
def biasedCoin(): | |
"This coin isbiased 30/70" | |
n = random.randrange(1,4) | |
return int(n == 1) | |
def makeFairCoin(coin): | |
def fair(): | |
while(1): | |
flip1 = coin() | |
flip2 = coin() | |
if (flip1 != flip2): | |
return flip1 | |
return fair | |
analyzeCoin(biasedCoin) | |
fairCoin = makeFairCoin(biasedCoin) | |
analyzeCoin(fairCoin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment