generated from mwc/lab_dice
65 lines
1.5 KiB
Python
65 lines
1.5 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 self in self.faces():
|
|
for count in range (1, 7):
|
|
if faces.count(count) >= 3:
|
|
return True
|
|
return False
|
|
|
|
def is_four_of_a_kind(self):
|
|
faces = self.faces()
|
|
for self in self.faces():
|
|
for count in range(1, 7):
|
|
if faces.count(count) >= 4:
|
|
return True
|
|
return False
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.all_ones():
|
|
successes += 1
|
|
print(successes/trials)
|
|
|
|
successes_three_of_a_kind = 0
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_three_of_a_kind():
|
|
successes_three_of_a_kind += 1
|
|
print("Odds of three of a kind", successes_three_of_a_kind / trials)
|
|
|
|
successes_four_of_a_kind = 0
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_four_of_a_kind():
|
|
successes_four_of_a_kind += 1
|
|
print("Odds of four of a kind", successes_four_of_a_kind / trials)
|
|
|
|
print(successes/trials)
|
|
|
|
|
|
|