[ create a new paste ] login | about

Link: http://codepad.org/kwzDkpDF    [ raw code | output | fork ]

aaronla - Python, pasted on Oct 22:
import random
import time


def analyzeCoin(coin, numSamples=1000):
    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 is biased 1:99"
    n = random.randrange(0,100)
    return int(n == 0)



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)


Output:
1
2
After 1000 flips, this coin landed heads 1% of the time and tails 98% of the time
After 1000 flips, this coin landed heads 49% of the time and tails 50% of the time


Create a new paste based on this one


Comments: