码迷,mamicode.com
首页 > 编程语言 > 详细

python应用-掷骰子模拟-pygal

时间:2018-09-29 13:09:12      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:def   svg   效果   file   install   info   imp   random   sid   

pygal安装:

Linux下:
pip install pygal
Windows下:
python -m pip install pygal

效果如图:
技术分享图片

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 28 23:07:29 2018
@author: pprp
"""

from random import randint
import pygal

class Die():
    def __init__(self,num_sides=6):
        self.num_sides=num_sides
        
    def roll(self):
        return randint(1,self.num_sides)
    
if __name__ == '__main__':    
    die1=Die()
    die2=Die()
    
    results=[]
    
    for roll_num in range(100000):
        result = die1.roll()+die2.roll()
        results.append(result)
        
        
    frequencies=[]
    max_result=die1.num_sides+die2.num_sides
    for val in range(2,max_result+1):
        frequency=results.count(val)
        frequencies.append(frequency)
        
    print(frequencies)
    
    hist=pygal.Bar()
    
    hist.title="Rolling one D6 1000 times"
    hist.x_labels=['2','3','4','5','6','7','8','9','10','11','12']
    hist.x_title="result"
    hist.y_title="Frequency"
    
    hist.add('D6 + D6',frequencies)
    hist.render_to_file('die_visual.svg')

python应用-掷骰子模拟-pygal

标签:def   svg   效果   file   install   info   imp   random   sid   

原文地址:https://www.cnblogs.com/pprp/p/9723068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!