Created
January 31, 2012 11:19
-
-
Save bpgergo/1709976 to your computer and use it in GitHub Desktop.
monte_carlo_simulation_in_ptyhon
This file contains 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
def do_n_flips(n): | |
return ''.join([str(random.getrandbits(1)) for i in range(n)]) |
This file contains 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
def monte_carlo_solve(n, k, j): | |
ret = 0.0 | |
for x in range(j): | |
ret = ret + payoff(n, k) | |
return ret / j |
This file contains 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
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