generated from mwc/lab_dice
dice_stats.py
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from die import Die
|
||||
from tqdm import tqdm
|
||||
from collections import Counter
|
||||
|
||||
class FiveDice:
|
||||
def __init__(self):
|
||||
self.dice = [Die() for number in range(5)]
|
||||
self.dice = [Die() for number in range(5)] # 5 die are cast at the same time, figuratively
|
||||
|
||||
def roll(self):
|
||||
for die in self.dice:
|
||||
@@ -13,21 +14,47 @@ class FiveDice:
|
||||
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):
|
||||
counts = Counter([die.face for die in self.dice]) # Counter class counts
|
||||
# each list item occurrance
|
||||
my_dict=dict(counts) # converts Counter class to a dict
|
||||
values = list(my_dict.values()) # unpacks dict values into a list
|
||||
for a in values: # loops in the list of dict values
|
||||
if a == 3: # exactly three die cast are the same number
|
||||
|
||||
return True # exactly three die cast are the same number
|
||||
|
||||
return False # other than three die cast are the same number
|
||||
|
||||
def is_four_of_a_kind(self):
|
||||
counts = Counter([die.face for die in self.dice]) # Counter class counts
|
||||
# each list item occurrance
|
||||
my_dict=dict(counts) # converts Counter class to a dict
|
||||
values = list(my_dict.values()) # unpacks dict values into a list
|
||||
for a in values: # loops in the list of dict values
|
||||
if a == 4: # exactly four die cast are the same number
|
||||
|
||||
return True # exactly four die cast are the same number
|
||||
|
||||
return False # other than four die cast are the same number
|
||||
|
||||
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
|
||||
|
||||
print(successes/trials)
|
||||
|
||||
|
||||
if dice.is_three_of_a_kind(): # if True
|
||||
successes_three += 1 # increment by 1
|
||||
if dice.is_four_of_a_kind(): # if True
|
||||
successes_four += 1 # increment by 1
|
||||
|
||||
print("exactly three of a kind: ")
|
||||
print(successes_three/trials) # decimal
|
||||
s3=successes_three/trials*100 # percent
|
||||
print(str(s3) + " %")
|
||||
print()
|
||||
print("exactly four of a kind: ")
|
||||
print(successes_four/trials) # decimal
|
||||
s4=successes_four/trials*100 # percent
|
||||
print(str(s4) + " %")
|
||||
|
||||
Reference in New Issue
Block a user