generated from mwc/lab_dice
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:
@@ -19,15 +19,40 @@ class FiveDice:
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user