首先做一个骰子游戏的类,其核心是通过randint,在1-6之间随机抽取一个数字。
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)
接着,做1000次试验,将结果放到一个列表(frequency)中
from die import Die
import pygal
die = Die()
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
frequencies = []
for value in range(1, die.num_sides + 1):
frequency = results.count(value)
frequencies.append(frequency)
hist = pygal.Bar()
hist.title = 'Results of rolling one D6 1000 times'
hist.x_labels = ['1','2','3','4','5','6']
hist.x_title = 'Result'
hist.y_title = 'Frequency of Result'
hist.add('D6', frequencies