fork download
  1. import random
  2.  
  3. def monte_carlo():
  4.  
  5. s = 10 * '0' + 10 * '1'
  6. L = list(s)
  7. count_in_church = 0
  8. RUNS = 10000
  9.  
  10.  
  11. for _ in range(RUNS):
  12. random.shuffle(L)
  13.  
  14. # check if bride steps into church by checking if
  15. # the number of ones exceeds the number of zeros
  16. cnt_0 = 0
  17. cnt_1 = 0
  18.  
  19. for c in L:
  20. if c == '0': cnt_0 += 1
  21. else: cnt_1 += 1
  22.  
  23. if cnt_1 > cnt_0:
  24. count_in_church += 1
  25. break
  26.  
  27.  
  28. total_permutations = RUNS
  29. print("number of times bride entered church: ", count_in_church)
  30. print("total permutations:", total_permutations)
  31. print("probability:", count_in_church / total_permutations)
  32.  
  33.  
  34. monte_carlo()
Success #stdin #stdout 0.12s 37616KB
stdin
Standard input is empty
stdout
number of times bride entered church:  9110
total permutations: 10000
probability: 0.911