pygal--模拟投骰子

pygal--模拟投骰子_第1张图片

截图来自<Python编程:从入门到实践>

die.py

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)

dice_visual.py

import pygal
from die import Die
die_1=Die()
die_2=Die()

results=[die_1.roll()+die_2.roll() for roll_num in range(1000)]
max_result=die_1.num_sides+die_2.num_sides
frequencies=[results.count(value) for value in range(2,max_result+1)]
hist=pygal.Bar()
hist.title = "Results of rolling two D6 dice 1000 times."
hist.x_labels = list(range(2,13))
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6 + D6', frequencies)
hist.render_to_file('dice_visual.svg')




你可能感兴趣的:(pygal--模拟投骰子)