Advertisement
Jacksilver

Probability simulation

Oct 29th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def run_test():
  2.     global count, N
  3.  
  4.     def run_experiment(n_balls, n_cells, offset):
  5.         cells = [0] * n_cells
  6.         # toss balls randomly to cells:
  7.         for j in range(n_balls):
  8.             cells[random.randrange(0, n_cells)] += 1
  9.             # cells[int(lines[offset + j])] += 1
  10.         cells = sorted(cells)
  11.         # print(cells)
  12.  
  13.         # check if there is an empty cell. if so return 0, otherwise 1:
  14.         if cells[0] == 0:
  15.             return 0
  16.         return 1
  17.    
  18.     count = 0
  19.     N = 1000000
  20.     offset = 0
  21.     N_CELLS = 8
  22.     N_BALLS = 12
  23.     # iterate experiment
  24.     for i in range(N):
  25.         result = run_experiment(N_BALLS, N_CELLS, offset=offset)
  26.         count += result
  27.         offset += N_CELLS
  28.  
  29.     print("probability:", count, "/", N, "(~", count / N, ")")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement