Skip to content

Commit 7f017da

Browse files
author
ehmatthes
committed
掷两个不同的骰子
1 parent f8dcaec commit 7f017da

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

模拟掷骰子/different_dice.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from die import Die
2+
import pygal
3+
4+
#创建一个D6和D10
5+
die_1 = Die()
6+
die_2 = Die(10)
7+
8+
# 掷几次骰子,并将结果存储在一个列表中
9+
results = []
10+
for roll_num in range(50000):
11+
result = die_1.roll() + die_2.roll()
12+
results.append(result)
13+
14+
# 分析结果
15+
frequencies = []
16+
max_result = die_1.num_sides + die_2.num_sides
17+
for value in range(2, max_result+1):
18+
frequency = results.count(value)
19+
frequencies.append(frequency)
20+
21+
# 对结果可视化
22+
hist = pygal.Bar()
23+
24+
hist.title = "Result of rolling one D6 1000 times."
25+
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
26+
'13', '14', '15', '16']
27+
hist.x_title = 'Result'
28+
hist.y_title = "Frequency of Result"
29+
30+
hist.add('D6 + D10', frequencies)
31+
hist.render_to_file('dice_visual.svg')

0 commit comments

Comments
 (0)