generated from mwc/lab_dice
From what i've understood, classes are supposed to make it easier to call on a group of objects/methods all at once. I think a good simulation to use classes for would be an epidemic. Person would be one class and the methods would be susceptible, infected, recovered. Virus and Interactions would be two more classes.
60 lines
1.4 KiB
Python
60 lines
1.4 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):
|
|
count={}
|
|
for face in self.faces():
|
|
count[face]=count.get(face, 0)+1
|
|
return any(count >= 3 for count in count.values())
|
|
def is_four_of_a_kind(self):
|
|
count={}
|
|
for face in self.faces():
|
|
count[face]=count.get(face, 0)+1
|
|
return any(count >= 4 for count in count.values())
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.all_ones():
|
|
successes += 1
|
|
print("all ones", successes/trials)
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_three_of_a_kind():
|
|
successes += 1
|
|
print("three of a kind", successes/trials)
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_four_of_a_kind():
|
|
successes += 1
|
|
print("four of a kind", successes/trials)
|
|
|
|
|