Skip to content

Instantly share code, notes, and snippets.

@Laharah
Last active June 18, 2018 21:58
Show Gist options
  • Save Laharah/31b2f76bf98d617528598f86829e7034 to your computer and use it in GitHub Desktop.
Save Laharah/31b2f76bf98d617528598f86829e7034 to your computer and use it in GitHub Desktop.
simulate the frogs riddle
import random
M = False # male frog you don't survive
F = True # female frog you do survive
def make_frogs():
'make the scenario, single frog is first, 2 frogs at s[1] and s[2]'
choices = [M, F] # male or female frog
s = []
for x in range(3):
s.append(random.choice(choices))
if all(s[1:]): # there has to be at least one male frog from index 1 and 2
return make_frogs() # if not, re-roll until there is
else:
return s
# first we pick only the single frog on the left s[0]
left = []
for _ in range(10000): # we do it 10k times
frogs = make_frogs()
left.append(frogs[0])
# next we pick one of 2 frogs on the right at random s[1] or s[2]
right = []
for _ in range(10000): # we do it 10k times
frogs = make_frogs()
right.append(frogs[random.randrange(1, len(frogs))])
# lets print the odds of survival:
print('choose left survival: ', sum(left) / len(left))
print('choose right survival: ', sum(right) / len(right))
@Laharah
Copy link
Author

Laharah commented Jun 18, 2018

results:
choose left survival: 0.4996
choose right survival: 0.3322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment