Skip to content

Instantly share code, notes, and snippets.

@bpgergo
Created January 31, 2012 11:19
Show Gist options
  • Save bpgergo/1709976 to your computer and use it in GitHub Desktop.
Save bpgergo/1709976 to your computer and use it in GitHub Desktop.
monte_carlo_simulation_in_ptyhon
def do_n_flips(n):
return ''.join([str(random.getrandbits(1)) for i in range(n)])
def monte_carlo_solve(n, k, j):
ret = 0.0
for x in range(j):
ret = ret + payoff(n, k)
return ret / j
def payoff(n, k):
if do_n_flips(n).find(''.rjust(k, '0')) > -1:
return 1.0
else:
return 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment