Advertisement
Guest User

16

a guest
Feb 29th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement