Skip to content

Instantly share code, notes, and snippets.

@AberrantWolf
Last active October 22, 2019 15:02
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 AberrantWolf/3697add1bff3377c44746e81f43c5178 to your computer and use it in GitHub Desktop.
Save AberrantWolf/3697add1bff3377c44746e81f43c5178 to your computer and use it in GitHub Desktop.
Brute force the average number of excess toys to expect from a whole McDonald's set if only half are offered at a time vs. all offered at once.
import secrets
from itertools import chain
NUM_TOYS = 10
NUM_TRIALS = 10000
def pick_toy(toys_set):
return secrets.choice(list(toys_set))
def collect_toys(the_set):
while any(count < 1 for count in the_set.values()):
toy = pick_toy(the_set)
the_set[toy] += 1
def run_single_test():
# Roll one set until you have at least one of each
whole_set = {el: 0 for el in range(NUM_TOYS)}
collect_toys(whole_set)
# print("WHOLE:" + str(whole_set))
# Roll first half of set
second_set_a = {el: 0 for el in range(NUM_TOYS // 2)}
collect_toys(second_set_a)
# Roll second half of set
second_set_b = {el: 0 for el in range(
NUM_TOYS - len(second_set_a), NUM_TOYS)}
collect_toys(second_set_b)
final_set = dict(chain.from_iterable(d.items()
for d in (second_set_a, second_set_b)))
# print("HALF :" + str(final_set))
return whole_set, final_set
if __name__ == "__main__":
whole_excess = 0
partial_excess = 0
for _ in range(NUM_TRIALS):
whole, partial = run_single_test()
for it in whole:
whole_excess += whole[it] - 1
for it in partial:
partial_excess += partial[it] - 1
print("Avg. WHOLE excess: " + str(whole_excess / NUM_TRIALS))
print("Avg. PART excess: " + str(partial_excess / NUM_TRIALS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment