Skip to content

Instantly share code, notes, and snippets.

@artlung
Created May 19, 2010 00:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artlung/405766 to your computer and use it in GitHub Desktop.
Save artlung/405766 to your computer and use it in GitHub Desktop.
Monte Carlo Python Program
"""Run a Monte Carlo simulation to validate the lottery example."""
import random
from lottery import winner, PEOPLE
def simulate(runs, f=winner, arg=PEOPLE):
"""Simulate function f with argument arg with number of given runs."""
# Store the number of results in a dict.
result_counts = {} # this is a dict
# Initialize the result counter.
for a in arg:
result_counts[a] = 0
for run in xrange(runs):
result = f(arg)
result_counts[result] += 1
return result_counts
if __name__ == '__main__':
# random.seed(0)
total_runs = 100000
people_counts = simulate(total_runs)
for k,v in people_counts.items():
print "%s: %d (%.02f%%)" % (k, v, (v / float(total_runs) * 100))
@artlung
Copy link
Author

artlung commented Jan 6, 2022

I do not recall the context, 12 years later, of why I saved this as a gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment