Files
lab_dice/dice_stats.py
mbhatti4 f4b2d95e02 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.
2025-11-11 14:46:14 -05:00

59 lines
1.3 KiB
Python

from die import Die
from tqdm import tqdm
class FiveDice:
def __init__(self):
self.dice = [Die() for number in range(5)]
def roll(self):
for die in self.dice:
die.roll()
return self.faces()
def faces(self):
return [die.face for die in self.dice]
def all_ones(self):
for face in self.faces():
if face != 1:
return False
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()
successes = 0
successes_three = 0
successes_four = 0
trials = 1000000
for trial in tqdm(range(trials)):
dice.roll()
if dice.all_ones():
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("Odds of three-of-a-kind:", successes_three / trials)
print("Odds of four-of-a-kind:", successes_four / trials)