Last active
November 1, 2017 18:17
-
-
Save ysimonson/7f259c1f5bbd895d844bc5893675dec8 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
""" | |
The script will run multiple simulations of flipping a coin continuously until | |
we hit either the sequence THHH or HHHH. It will print out how many times each | |
sequence was hit. | |
""" | |
# Use `secrets` rather than `random` because this is guaranteed to generate | |
# cryptographically secure random values | |
import secrets | |
def main(): | |
current_sequence = [] | |
hhhh = 0 | |
thhh = 0 | |
for _ in range(100000): | |
current_sequence.append(secrets.randbelow(2) == 1) | |
last_four = current_sequence[-4:] | |
if last_four == [True, True, True, True]: | |
hhhh += 1 | |
current_sequence = [] | |
elif last_four == [False, True, True, True]: | |
thhh += 1 | |
current_sequence = [] | |
print("hhhh=%s\tthhh=%s\tratio=%s" % (hhhh, thhh, hhhh / thhh)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment