机器学习之掷骰子的Python程序

首先用pip 工具安装模块pygal
import pygal
from random import randint
class Die():
def init(self,num_sides=6):
self.num_sides=num_sides
def roll(self):
return randint(1,self.num_sides)
die_1=Die()
die_2=Die(10)
results=[]
for roll_num in range(50000):
result=die_1.roll()+die_2.roll()
results.append(result)

print(results)

frequencies=[]
max_results=die_1.num_sides+die_2.num_sides
for value in range(2,max_results+1):
frequency=results.count(value)
frequencies.append(frequency)
print(frequencies)

hist=pygal.Bar()
hist.title=‘results of rolling two D6 dices 1000 times.’
hist.xlabels=[‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’, ‘8’, ‘9’, ‘10’, ‘11’, ‘12’,‘13’, ‘14’, ‘15’, ‘16’]
hist.x_title=‘results’
hist.y_title=‘frequency of result’
hist.add(‘D6+D10’,frequencies)
hist.render_to_file(‘dice_visual.svg’)

展开阅读全文

没有更多推荐了,返回首页