Created
September 18, 2016 07:23
-
-
Save martcous/acd462ff6448c4b265987d4526089492 to your computer and use it in GitHub Desktop.
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
import random | |
# In this code, we try to evaluate the probability of, when | |
# rolling 2 dice, having a second 6 after having a first 6. | |
num_tries = 1000000 | |
tries = 0 | |
win = 0 | |
while tries < num_tries: | |
d1 = random.randint(1,6) | |
d2 = random.randint(1,6) | |
# We need at least one 6 for the experiment. | |
if d1 != 6 and d2 != 6: | |
continue | |
# Get the other die. | |
if d1 == 6: | |
d = d2 | |
else: | |
d = d1 | |
# If it's a 6, we have a successful try. | |
if d == 6: | |
win += 1 | |
tries += 1 | |
ratio = 1. * win / tries | |
print("Success rate: {0}".format(ratio)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment