Guest User

diceRollHistogram

a guest
Dec 4th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import random
  2. from collections import defaultdict
  3.  
  4. def main():
  5.     rolls = int(input("How many rolls? "))
  6.     dice = int(input("How many dice? "))
  7.     sides = int(input("How many sides per die? "))
  8.  
  9.     d = roll(dice,sides,rolls)
  10.  
  11.     h = make_histogram(d)
  12.  
  13.     display_output(d,h)
  14.  
  15. def roll(dice,sides,rolls):
  16.     output_dict = defaultdict(int)
  17.     for _ in range(rolls):
  18.         output_dict[sum([random.randint(1,sides) for _ in range(dice)])] += 1
  19.     return output_dict
  20.  
  21. def make_histogram(roll_array,total_symbols=80):
  22.     total_rolls = sum([roll_array[i] for i in roll_array])
  23.     output_array = defaultdict(int)
  24.     for i in roll_array:
  25.         output_array[i] = roll_array[i]*total_symbols/total_rolls
  26.     return output_array
  27.  
  28. def display_output(d,h):
  29.     total_rolls = sum([d[i] for i in d])
  30.     for i in d:
  31.         print('{:2d}{:10d}{:8.2%} {}'.format(i,d[i],d[i]/total_rolls,"#"*int(h[i])))
  32.    
  33. main()
Advertisement
Add Comment
Please, Sign In to add comment