Skip to content

Instantly share code, notes, and snippets.

@martcous
Created September 18, 2016 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martcous/acd462ff6448c4b265987d4526089492 to your computer and use it in GitHub Desktop.
Save martcous/acd462ff6448c4b265987d4526089492 to your computer and use it in GitHub Desktop.
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