Skip to content

Instantly share code, notes, and snippets.

@colinpollock
Created October 27, 2012 00:26
Show Gist options
  • Save colinpollock/3962411 to your computer and use it in GitHub Desktop.
Save colinpollock/3962411 to your computer and use it in GitHub Desktop.
""" 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