I edited dice_stats.py to add the additional trials

You can use Classes to simmulate many experiments. Just as we used it to roll dice, we could also simulate randomizer picking numbers to then do something with.
You could flip coins and then have outcomes such as "three heads in a row". You can also use it to simulate shuffle a deck of cards and have it simulate giving them out.
This commit is contained in:
mbhatti4
2025-11-11 14:46:14 -05:00
parent 6c96e08130
commit f4b2d95e02

View File

@@ -19,15 +19,40 @@ class FiveDice:
return False return False
return True return True
def is_three_of_a_kind(self):
faces = self.faces()
for value in set(faces):
if faces.count(value) >= 3:
return True
return False
def is_four_of_a_kind(self):
faces = self.faces()
for value in set(faces):
if faces.count(value) >= 4:
return True
return False
dice = FiveDice() dice = FiveDice()
successes = 0 successes = 0
successes_three = 0
successes_four = 0
trials = 1000000 trials = 1000000
for trial in tqdm(range(trials)): for trial in tqdm(range(trials)):
dice.roll() dice.roll()
if dice.all_ones(): if dice.all_ones():
successes += 1 successes += 1
if dice.is_three_of_a_kind():
successes_three += 1
if dice.is_four_of_a_kind():
successes_four += 1
print(successes/trials) print(successes/trials)
print("Odds of three-of-a-kind:", successes_three / trials)
print("Odds of four-of-a-kind:", successes_four / trials)