pygal模拟投掷骰子
1 import pygal 2 import random 3 4 5 class Die(object): 6 def __init__(self, num, side_num): 7 self.num = num 8 self.side_num = side_num 9 self.hist = pygal.Bar() 10 11 def throw_times_and_fre(self): 12 for i in range(self.num): 13 num = random.randint(1, 6) 14 results.append(num) 15 for i in range(1, 7): 16 frequency = results.count(i) 17 frequencies.append(frequency) 18 19 def run(self): 20 self.hist.x_labels = ["1", "2", "3", "4", "5", "6"] 21 self.hist.x_title = "点数" 22 self.hist.y_title = "频数" 23 self.hist.add("D6", frequencies) 24 self.hist.render_to_file("gkl.svg") 25 26 27 if __name__ == '__main__': 28 results = [] 29 frequencies = [] 30 die = Die(100, 6) 31 die.throw_times_and_fre() 32 die.run()