Created
October 27, 2012 00:26
-
-
Save colinpollock/3962411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Prints the probability of a successful shot given argv[1] attempts where | |
each has an argv[2] probability of success. | |
""" | |
from __future__ import division | |
import random | |
import sys | |
def yes_no(prob): | |
return random.random() < prob | |
def prob_of_hitting(num_shots, prob_for_each_shot): | |
return any(yes_no(prob_for_each_shot) for _ in xrange(num_shots)) | |
if __name__ == '__main__': | |
num_shots = int(sys.argv[1]) | |
prob_of_each = float(sys.argv[2]) | |
trials = int(sys.argv[3]) | |
successes = sum(prob_of_hitting(num_shots, prob_of_each) | |
for _ in xrange(trials)) | |
print successes / trials | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment