Created
March 20, 2024 14:59
-
-
Save DonSheddow/73ab9599947522b35c16644f9f7f0efd to your computer and use it in GitHub Desktop.
Ann's problem
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
from math import comb | |
import sys | |
import random | |
s1, s2, s3, s4 = 10, 11, 12, 13 | |
h = 7 | |
N = s1 + s2 + s3 + s4 | |
def generate_deck(): | |
deck = s1*[1] + s2*[2] + s3*[3] + s4*[4] | |
return deck | |
def pick_card(deck): | |
idx = random.randrange(len(deck)) | |
return deck.pop(idx) | |
def sample_is_flush(): | |
deck = generate_deck() | |
hand = [] | |
for _ in range(h): | |
hand.append(pick_card(deck)) | |
return any(hand.count(suit) >= 5 for suit in [1, 2, 3, 4]) | |
def calc_probability(): | |
sum_ = 0 | |
for si in [s1, s2, s3, s4]: | |
sum_ += sum(comb(si, k) * comb(N - si, h - k) for k in range(5, h+1)) | |
return sum_ / comb(N, h) | |
if __name__ == '__main__': | |
samples = int(sys.argv[1]) | |
flushes = 0 | |
for n in range(samples): | |
flushes += int(sample_is_flush()) | |
print(f"{flushes} out of {samples} was a flush") | |
print(flushes / samples) | |
print() | |
print("calculation gives probabilty", calc_probability()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment