Created
January 15, 2014 08:48
-
-
Save understar/8432868 to your computer and use it in GitHub Desktop.
Simple roll sides script.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Jan 08 18:02:10 2014 | |
@author: understar | |
""" | |
from __future__ import division | |
from collections import Counter | |
import sys | |
from random import randint | |
# Python 2/3 compatibility | |
if sys.hexversion >= 0x3000000: | |
inp = input | |
rng = range | |
else: | |
inp = raw_input | |
rng = xrange | |
def get_int(prompt): | |
while True: | |
try: | |
return int(inp(prompt)) | |
except ValueError: | |
pass | |
def count_double_one(sources): | |
count = {} | |
start = False | |
gaps = 0 | |
for val in sources: | |
if val == 1: | |
if not start: | |
start = True | |
else: | |
gaps += 1 | |
if count.has_key(gaps): | |
count[gaps] += 1 | |
else: | |
count[gaps] = 1 | |
gaps = 0 | |
#start = False | |
else: | |
gaps += 1 | |
return count | |
if __name__=="__main__": | |
import numpy as np | |
import matplotlib.pyplot as plt | |
sides = get_int("How many sides does your die have? ") | |
times = get_int("How many times do you want to roll? ") | |
sources = [randint(1, sides) for roll in rng(times)] | |
results = Counter(sources) | |
counts = count_double_one(sources) | |
E = np.sum(np.array(counts.keys())*(np.array(counts.values())/np.sum(counts.values()))) | |
print 'E is %f' % E | |
plt.bar(counts.keys(),counts.values()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment