Skip to content

Instantly share code, notes, and snippets.

@Amit2016-17
Forked from artlung/monte_carlo.py
Created January 10, 2022 12:53
Show Gist options
  • Select an option

  • Save Amit2016-17/dd9c53a03a98ef1b38ce6ff4d593419f to your computer and use it in GitHub Desktop.

Select an option

Save Amit2016-17/dd9c53a03a98ef1b38ce6ff4d593419f 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment