Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

16

By: a guest on Feb 29th, 2012  |  syntax: Python  |  size: 1.31 KB  |  views: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. import sys, random
  4.  
  5. # finds the highest occuring roll (should almost always be 7)
  6. def common_roll(history):
  7.   highest = (0,0)
  8.   for k in history:
  9.     if history[k] > highest[1]:
  10.       highest = (k, history[k])
  11.   return highest
  12.  
  13. def roll(history):
  14.   r = random.randint(1, 6) + random.randint(1, 6)
  15.   if r not in history:
  16.     history[r] = 0
  17.   history[r] += 1
  18.   return r
  19.  
  20. def craps(history, num):
  21.   wins = 0
  22.   losses = 0
  23.   rolls = []
  24.   for x in xrange(num):
  25.     point = 0
  26.     r = roll(history)
  27.     n_rolls = 1
  28.     if r in (2, 3, 12):
  29.       losses += 1
  30.     elif r in (7, 11):
  31.       wins += 1
  32.     else:
  33.       point = r
  34.       r = roll(history)
  35.       n_rolls += 1
  36.       while r not in (point, 7):
  37.         r = roll(history)
  38.         n_rolls += 1
  39.       if r == point:
  40.         wins += 1
  41.       elif r == 7:
  42.         losses += 1
  43.     rolls.append(n_rolls)
  44.   return (wins, losses), rolls
  45.  
  46. if __name__ == '__main__':
  47.   n = int(sys.argv[1])
  48.   history = {}
  49.   wl, rolls = craps(history,n)
  50.   print 'Most common roll (roll, occurances):  %d: %d' % common_roll(history)
  51.   print 'Average winning percentage: {}%'.format(float(wl[0]) / n * 100)
  52.   print 'Average number of rolls in a game: {}'.format(
  53.       sum(rolls) / float(len(rolls)))
  54.   print 'Maximum rolls: {}'.format(max(rolls))
clone this paste RAW Paste Data