Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def run_trial(num_prisoners = 100, attempt_max = 50):
- boxes = range(num_prisoners)
- if attempt == attempt_max: break
- attempt += 1
- else: #did not break, prisoner bfound his box
- found +=1
- return found
- def run_trials(num_prisoners = 100, attempt_max = 50, num_trials = 1000):
- print "running %i trials with %i prisoners and %i attempts each"%(num_trials, num_prisoners, attempt_max)
- 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
- percent_success = run_trials()
- print "all prisoners will get their number %f%% of the time"%percent_success
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement