This file contains 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 | |
| def toss_coin(): | |
| return random.randint(0, 1) | |
| def roll_die(): | |
| return random.randint(1, 6) | |
| def pick_1_from_n_cards(n): | |
| return random.randint(1, n) | |
| digits = [None, None, None, None] | |
| sequence = "".join(["H" if toss_coin() else "T" for _ in range(7)]) | |
| if sequence in ["HHHHHHH", "TTTTTTT", "HTTTTTT"]: # you could early-exit this if you hit TH or HTH etc. | |
| # >= 2000 (48/2048 = 3/128 chance) | |
| digits[0] = 2 | |
| digits[1] = 0 | |
| if roll_die() == 1: | |
| # >= 2040 (8/48 = 1/6 chance) | |
| digits[2] = 4 | |
| digits[3] = pick_1_from_n_cards(8) - 1 | |
| else: | |
| digits[2] = pick_1_from_n_cards(4) - 1 | |
| digits[3] = pick_1_from_n_cards(10) - 1 | |
| else: | |
| # < 2000 | |
| digits[0] = toss_coin() | |
| digits[1] = pick_1_from_n_cards(10) - 1 | |
| digits[2] = pick_1_from_n_cards(10) - 1 | |
| digits[3] = pick_1_from_n_cards(10) - 1 | |
| print("".join(map(str, digits))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment