Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
from __future__ import division
import random
class Die(object):
"""The Gambler's Fallacy made manifest"""
def __init__(self, size):
self.size = size
self.state = [1 for i in xrange(size)]
def roll(self):
random_mark = random.random() * sum(self.state)
# random_mark = random.randint(1,6)
# print "Random mark: {}".format(random_mark)
running_sum = 0
result = -1
for roll, mark in enumerate(self.state):
# mark = self.state[roll]
running_sum += mark
if random_mark < running_sum and result == -1:
result = roll
self.state[roll] = 1
else:
self.state[roll] += 1
return result + 1
def test():
die = Die(6)
result = [0,0,0,0,0,0]
for i in xrange(1000000):
q = die.roll() - 1
# print q
result[q] += 1
average = 1000000 / 6
randomness = [abs(i - average) / 1000000 for i in result]
print result
for i, r in enumerate(randomness, 1):
print "Roll {} deviated from the average {:0.6f}% of the time".format(i, r)
if __name__ == "__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.