generated from mwc/lab_dice
An example could be playing cards. Each card is represented as a suit or value (1,2,3,4,...) and a deck is collection of cards. A player class can keep track of the cards and score. You could analyze or run the simulation multiple times to see which player wins the most or if a player is more likely to pick up a certain suit.
This commit is contained in:
@@ -19,15 +19,45 @@ class FiveDice:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def face_counts(self):
|
||||||
|
counts = {}
|
||||||
|
for face in self.faces():
|
||||||
|
if face in counts:
|
||||||
|
counts[face] += 1
|
||||||
|
else:
|
||||||
|
counts[face] = 1
|
||||||
|
return counts
|
||||||
|
|
||||||
|
def is_three_of_a_kind(self):
|
||||||
|
for count in self.face_counts().values():
|
||||||
|
if count >= 3:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_four_of_a_kind(self):
|
||||||
|
for count in self.face_counts().values():
|
||||||
|
if count >= 4:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
dice = FiveDice()
|
dice = FiveDice()
|
||||||
successes = 0
|
all_ones_count = 0
|
||||||
|
three_kind_count = 0
|
||||||
|
four_kind_count = 0
|
||||||
trials = 1000000
|
trials = 1000000
|
||||||
for trial in tqdm(range(trials)):
|
for trial in tqdm(range(trials)):
|
||||||
dice.roll()
|
dice.roll()
|
||||||
if dice.all_ones():
|
if dice.all_ones():
|
||||||
successes += 1
|
all_ones_count += 1
|
||||||
|
if dice.is_three_of_a_kind():
|
||||||
|
three_kind_count += 1
|
||||||
|
if dice.is_four_of_a_kind():
|
||||||
|
four_kind_count += 1
|
||||||
|
|
||||||
|
print("all ones:", all_ones_count/trials)
|
||||||
|
print("Three of a kind:", three_kind_count / trials)
|
||||||
|
print("Four of a kind:", four_kind_count / trials)
|
||||||
|
|
||||||
print(successes/trials)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user