Skip to content

Instantly share code, notes, and snippets.

@tessprime
Last active June 12, 2019 07:12
Show Gist options
  • Select an option

  • Save tessprime/cf79861284744c33446b6711fde551bf to your computer and use it in GitHub Desktop.

Select an option

Save tessprime/cf79861284744c33446b6711fde551bf to your computer and use it in GitHub Desktop.
import random
def generate_pairs(n):
return [(random.choice(["b","g"]), random.choice(["b","g"])) for i in range(n)]
def generate_children(n):
return [random.choice(["b","g"]) for i in range(n)]
def flatten(alist):
result = []
for group in alist:
result += group
return result
# Scenario 1
# pick a pair, choose a child at random
# if it's not a boy, then this didn't happen (continue),
# otherwise check the other
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
choice = random.choice(pair)
# if it's not a boy, this didn't happen
if choice == 'b':
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 1:", count, count_both, count_both/count)
# scenario 2
# pick a pair with at least one boy.
# If the oldest isn't a boy, this didn't happen
# check if they're both boys.
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
# eldest must be a boy
if 'b' not in pair:
continue
if pair[1] != 'b':
continue
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 2:", count, count_both, count_both/count)
# scenario 2.5
# pick a pair with at least one boy. If the youngest isn't a boy, this didn't happen
# check if they're both boys.
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
# eldest must be a boy
if 'b' not in pair:
continue
if pair[0] != 'b':
continue
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 2.5:", count, count_both, count_both/count)
# scenario 3
# pick a pair, if neither are boys, this didn't happen,
# then pick a child at random,
# if it's not a boy, then this didn't happen
# then check if both are boys
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
# filter to boys
if 'b' not in pair:
continue
# pick a child at random
choice = random.choice(list(enumerate(pair)))
# if it's not a boy, this didn't happen
if choice[1] == 'b':
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 3:", count, count_both, count_both/count)
# scenario 4
# pick a pair, if neither are boys, this didn't happen,
# and then check if they're the same
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
if 'b' not in pair:
continue
# count it and move on
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 4:", count, count_both, count_both/count)
# scenario 5
# pick a child, if it's not a boy, this didn't happen
# then check if the other child in the pair is the same
count = 0
count_both = 0
pairs = generate_pairs(100000)
children = flatten(pairs)
indexed_children = list(enumerate(children))
while count < 10000:
# (index, gender)
indexed_child = random.choice(indexed_children)
if indexed_child[1] != 'b':
continue
count += 1
index = indexed_child[0]
if index % 2 == 0:
if children[index] == children[index + 1]:
count_both += 1
else:
if children[index] == children[index - 1]:
count_both +=1
print("Scenario 5:", count, count_both, count_both/count)
# scenario 6
# pick a pair, if neither are boys, this didn't happen
# except 25% of the time it does. Then check if they're
# the same.
count = 0
count_both = 0
pairs = generate_pairs(100000)
while count < 10000:
pair = random.choice(pairs)
if 'b' not in pair and random.uniform(0,1) > .25:
continue
# count it and move on
count += 1
if pair[0] == pair[1]:
count_both += 1
print("Scenario 6:", count, count_both, count_both/count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment