Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. def run_trial(num_prisoners = 100, attempt_max = 50):
  4.     boxes = range(num_prisoners)
  5.             if attempt == attempt_max: break
  6.             attempt += 1
  7.         else: #did not break, prisoner bfound his box
  8.                 found +=1
  9.     return found
  10.  
  11. def run_trials(num_prisoners = 100, attempt_max = 50, num_trials = 1000):
  12.     print "running %i trials with %i prisoners and %i attempts each"%(num_trials, num_prisoners, attempt_max)
  13.     return sum([1 if trial == num_prisoners else 0 for trial in [run_trial(num_prisoners = num_prisoners, attempt_max=attempt_max) for i in range(num_trials)]])/float(num_trials)*100
  14.  
  15. percent_success = run_trials()
  16. print "all prisoners will get their number %f%% of the time"%percent_success
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement