Notebook
In [21]:
from random import SystemRandom

class RingBuffer:
    def __init__(self, size):
        self.data = [None for i in xrange(size)]

    def append(self, x):
        self.data.pop(0)
        self.data.append(x)

    def get(self):
        return self.data

FLIPS_PER_MINUTE = 4
TOTAL_TIME_MINUTES = 30
TOTAL_FLIPS = TOTAL_TIME_MINUTES*FLIPS_PER_MINUTE
HEAD_BIAS = 0.6 # X% likely to land heads
WARMUP_FLIPS = 10
TAILS = 'T'
HEADS = 'H'

rng = SystemRandom()
flips = RingBuffer(WARMUP_FLIPS)
balance = 25
next_bet = 1

def flip(head_bias):
    num = rng.random()
    if num > head_bias:
        return TAILS
    else:
        return HEADS

for i in range(WARMUP_FLIPS):
    flips.append(flip(HEAD_BIAS))

num_occurs = 0
    
for i in range(TOTAL_FLIPS - WARMUP_FLIPS):
    num_heads = flips.get().count(HEADS)
    ratio_heads = num_heads*1.0 / len(flips.get())
    bet = ratio_heads < HEAD_BIAS and next_bet or None
    result = flip(HEAD_BIAS)
    flips.append(result)
    
    if bet is not None:
        if result == HEADS:
            balance += bet
        else:
            balance -= bet

print(balance)
36