Advertisement
lightningleaf

card simulation

Nov 11th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. from random import randint # randint(1, 13) returns a random integer between 1 and 13 inclusive.
  2. from itertools import groupby # groups consecutive identical values of an iterable together
  3.  
  4. N = 5000 # number of simulations
  5.  
  6. def success(cards) :
  7.     groups = list(list(g) for k,g in groupby(sorted(cards))) # ranks grouped...
  8.     groups = list(reversed(sorted(groups, key=len))) # ... and sorted by decreasing length
  9.     return len(groups[0]) == 3 and len(groups[1]) == 1 # implies len(groups[2]) == 1
  10.  
  11. def test_success() :
  12.     assert success([5,5,5,7,6])
  13.     assert success([4,3,4,1,4])
  14.     assert not success([1,2,3,4,5])
  15.     assert not success([3,3,3,4,4])
  16.  
  17. random_hand_ranks = lambda : [randint(1, 13) for _ in range(5)] # 5 random integers between 1 and 13 inclusive
  18.  
  19. def sim(store=True) :
  20.     x_coords = []
  21.     y_coords = []
  22.     cnt = 0
  23.     for x in range(N) :
  24.         if success(random_hand_ranks()) :
  25.             cnt += 1
  26.         if store :
  27.             x_coords.append(x)
  28.             y_coords.append(cnt)
  29.     return (x_coords, y_coords) if store else cnt
  30.  
  31. if input('Show graph? (Y)') == 'Y' :
  32.     x,y = sim()
  33.     import matplotlib.pyplot as plt
  34.     plt.plot(x,y)
  35.     plt.ylabel('Simulation succeses')
  36.     plt.xlabel('Trials')
  37.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement