-
Notifications
You must be signed in to change notification settings - Fork 4
/
three_dice.py
33 lines (27 loc) · 997 Bytes
/
three_dice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Author: Bojan G. Kalicanin
# Date: 24-Dec-2016
# 15-8. Three Dice: If you roll three D6 dice, the smallest number you
# can roll is 3 and the largest number is 18. Create a visualization
# that shows what happens when you roll three D6 dice.
import pygal
from die import Die
# Create three D6 dice.
die_1 = Die()
die_2 = Die()
die_3 = Die()
# Roll the dice 1000 times.
results = list()
[results.append(die_1.roll() + die_2.roll() + die_3.roll())
for num_roll in range(1000)]
# Analyze the results.
frequencies = list()
max_result = die_1.num_sides + die_2.num_sides + die_3.num_sides
[frequencies.append(results.count(value)) for value in range(3, max_result+1)]
# Create visualization for the rolls.
hist = pygal.Bar()
hist.title = "Results of rolling three D6 dice for 1,000 times."
hist.x_labels = [str(i) for i in range(3, max_result+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add("D6 + D6 + D6", frequencies)
hist.render_to_file("three_dice_visual.svg")