python小象学院:random/zip()/matplotlib,直方图/散点图/numpy向量化操作,简单数据分析 -----模拟骰子

1.案例描述


2.使用函数

random

enumerate

zip

matplotlib

numpy

 


3.实验程序

"""
    auther:Susan
    function:
    1.Simulated throwing dice
    version:v1.0
    data:2019/4/20
"""

import random

def roll_dice():
    roll = random.randint(1,6)
    return roll

def main():

    total_time = 100000
    #Initialization list[0,0,0,0,0,0]
    result_list = [0]*6


    for i in range(total_time):
        roll = roll_dice()
        for j in range(1,7):
            if roll == j:
                result_list[j-1] +=1

    # print(result_list)

    for i,result in enumerate(result_list):
        print('Points: {}, Number of occurrences: {}, Frequency: {} '.format(i+1,result,result/total_time))


if __name__ == '__main__':
    main()

"""
    auther:Susan
    function:
    1.Simulated throwing one dice
    2.Simulated throwing two dice
    version:v2.0
    data:2019/4/20
"""

import random

def roll_dice():
    roll = random.randint(1,6)
    return roll

def main():

    total_time = 100000
    #Initialization list[0,0,0,0,0,0]

    result_list = [0]*11

    #Initianlization points' list
    roll_list = list(range(2,13))
    roll_dict = dict(zip(roll_list,result_list))

    for i in range(total_time):
        roll1 = roll_dice()
        roll2 = roll_dice()

        for j in range(2,13):
            if (roll1+roll2) == j:
                roll_dict[j] +=1

    # print(result_list)

    for i,result in roll_dict.items():
        print('Points: {}, Number of occurrences: {}, Frequency: {} '.format(i,result,result/total_time))


if __name__ == '__main__':
    main()

"""
    auther:Susan
    function:
    1.Simulated throwing one dice
    2.Simulated throwing two dice
    3.Data visualization
    version:v3.0
    data:2019/4/20
"""

import random
import matplotlib.pyplot as plt

def roll_dice():
    roll = random.randint(1,6)
    return roll

def main():

    total_time = 100
    #Initialization list[0,0,0,0,0,0]

    result_list = [0]*11

    #Initianlization points' list
    roll_list = list(range(2,13))
    roll_dict = dict(zip(roll_list,result_list))

    roll1_list = []
    roll2_list = []
    for i in range(total_time):
        roll1 = roll_dice()
        roll2 = roll_dice()

        roll1_list.append(roll1)
        roll2_list.append(roll1)

        for j in range(2,13):
            if (roll1+roll2) == j:
                roll_dict[j] +=1
                break
    # print(result_list)

    for i,result in roll_dict.items():
        print('Points: {}, Number of occurrences: {}, Frequency: {} '.format(i,result,result/total_time))

    # Data visualization
    x = range(1,total_time+1)
    plt.scatter(x,roll1_list,c='red',alpha=0.5)
    plt.scatter(x,roll2_list,c='green',alpha=0.5)
    plt.show()


if __name__ == '__main__':
    main()

"""
    auther:Susan
    function:
    1.Simulated throwing one dice
    2.Simulated throwing two dice
    3.Data visualization:scatter
    4.Data visualization:hist
    version:v4.0
    data:2019/4/20
"""

import random
import matplotlib.pyplot as plt

#Solve Chinese display problems
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False

def roll_dice():
    roll = random.randint(1,6)
    return roll

def main():

    total_time = 10000

    roll_list = []

    for i in range(total_time):
        roll1 = roll_dice()
        roll2 = roll_dice()
        roll_list.append(roll1+roll2)

    # Data visualization
    plt.hist(roll_list, bins=range(2,14), density=1, edgecolor='black', linewidth=1)

    plt.title('Dice point statistics')
    plt.xlabel('Points')
    plt.ylabel('Frequency')
    plt.show()
    #Chinese display
    # plt.title('骰子点数统计')
    # plt.xlabel('点数')
    # plt.ylabel('频率')
    # plt.show()

if __name__ == '__main__':
    main()
"""
    auther:Susan
    function:
    1.Simulated throwing one dice
    2.Simulated throwing two dice
    3.Data visualization
    4.Data visualization:hist
    5.Use numpy
    version:v5.0
    data:2019/4/21
"""

import matplotlib.pyplot as plt
import numpy as np

def main():

    total_time = 10000

    roll_list = []

    roll1_arr = np.random.randint(1,7,size=total_time)
    roll2_arr = np.random.randint(1,7,size=total_time)
    result_arr = roll1_arr + roll2_arr

    hist,bins = np.histogram(result_arr, bins=(2,14))
    print(hist)
    print(bins)


    # Data visualization
    plt.hist(result_arr, bins=range(2,14), density=1, edgecolor='black', linewidth=1,rwidth=0.8)
    # Set the x-axis coordinate display
    tick_labels = ['2 p','3 p','4 p','5 p',
                   '6 p','7 p','8 p','9 p','10 p','11 p','12 p']
    tick_pos = np.arange(2,13)+0.5
    plt.xticks(tick_pos,tick_labels)

    plt.title('Dice point statistics')
    plt.xlabel('Points')
    plt.ylabel('Frequency')
    plt.show()

if __name__ == '__main__':
    main()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值