Advertisement
Guest User

Card Random Walk

a guest
Aug 29th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4. def distance(points):
  5.     p0, p1 = points
  6.     return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
  7.  
  8. trials = 10000
  9. cards = [{'value':value+1,'suit':suit} for value in range(13) for suit in range(4)]
  10. counts = [{'total':0, 'count':0} for i in range(len(cards))]
  11. for i in range(trials):
  12.     start = (0,0)
  13.     location = start
  14.     cardscopy = list(cards)
  15.     random.shuffle(cardscopy)
  16.     for c in range(len(cardscopy)):
  17.         card = cardscopy[c]
  18.         if card['suit'] == 0:
  19.             location = (location[0]+card['value'], location[1])
  20.         elif card['suit'] == 1:
  21.             location = (location[0]-card['value'], location[1])
  22.         elif card['suit'] == 2:
  23.             location = (location[0], location[1]+card['value'])
  24.         else:
  25.             location = (location[0], location[1]-card['value'])
  26.         dist = distance((start,location))
  27.         counts[c]['total'] += dist
  28.         counts[c]['count'] += 1
  29. for i in range(len(counts)):
  30.     count = counts[i]
  31.     print 'Step %s average distance: %s' % (i+1, count['total'] / count['count'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement