Last active
September 13, 2024 17:31
-
-
Save lapp0/f665cd49686051451a89d3f50b1b5688 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import collections | |
def trial(first_to=2): | |
flips = [random.choice([True, False]) for _ in range(100)] | |
alice_flip_checks = flips | |
bob_flip_checks = [flips[i] for i in range(1, len(flips), 2)] + [flips[i] for i in range(0, len(flips), 2)] | |
# get second_head | |
try: | |
alice_second_head_idx = [i for i, value in enumerate(alice_flip_checks) if value][first_to-1] | |
except IndexError: | |
alice_second_head_idx = len(flips) + 1 | |
try: | |
bob_second_head_idx = [i for i, value in enumerate(bob_flip_checks) if value][first_to-1] | |
except IndexError: | |
bob_second_head_idx = len(flips) + 1 | |
if alice_second_head_idx > bob_second_head_idx: | |
return "bob" | |
elif alice_second_head_idx == bob_second_head_idx: | |
return "tie" | |
else: | |
return "alice" | |
def simulate(first_to=2, num_flips=100, num_trials=1000000): | |
counts = collections.Counter([trial(50) for _ in range(num_trials)]) | |
return {k: v / num_trials for k, v in counts.items()} | |
print("first to 2") | |
print(simulate(first_to=2)) | |
print("first to 50") | |
print(simulate(first_to=50)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment