毕业论文写作

毕业设计需求

计算机毕业设计中用python设计模拟抛掷骰子小程序

 案例描述

• 通过计算机程序模拟抛掷骰子,并显示各点数的出现次数及频率
• 比如,抛掷2个骰子50次,出现点数为7的次数是8,频率是0.16

版本1.0

1.0功能:模拟抛掷1个骰子,并输出其结果

如何通过Python模拟随机事件?或者生成随机数?
• random模块
• 遍历列表时,如何同时获取每个元素的索引号及其元素值?
• enumerate()函数

更多random模块的方法请参考:
https://docs.python.org/3/library/random.html

  1. '''

  2. 功能:模拟掷骰子

  3. 版本:1.0

  4. '''

  5. import random

  6. def roll_dice():

  7. '''

  8. 模拟掷骰子

  9. '''

  10. roll = random.randint(1,6)

  11. return roll

  12.  

  13. def main():

  14. total_times = 10

  15. #初始化列表[0,0,0,0,0,0]

  16. result_list = [0] * 6

  17.  

  18. for i in range(total_times ):

  19. roll = roll_dice()

  20. for j in range(1,7):

  21. if roll == j:

  22. result_list [j-1] += 1

  23. for i, result in enumerate(result_list):

  24. print('点数{}的次数:{},频率:{}'.format(i + 1, result, result / total_times))

  25. if __name__ == '__main__':

  26. main()

 版本2.0

功能:模拟抛掷2个骰子,并输出其结果

如何将对应的点数和次数关联起来?
• zip()函数

  1. '''

  2. 功能:模拟掷骰子

  3. 版本:2.0

  4. '''

  5. import random

  6. def roll_dice():

  7. '''

  8. 模拟掷骰子

  9. '''

  10. roll = random.randint(1,6)

  11. return roll

  12.  

  13. def main():

  14. total_times = 100

  15. #初始化列表[0,0,0,0,0,0]

  16. result_list = [0] * 11

  17.  

  18. #初始化点数列表

  19. roll_list = list(range(2,13))

  20.  

  21. roll_dict = dict(zip(roll_list ,result_list )) #元组结构

  22.  

  23. for i in range(total_times ):

  24. roll1 = roll_dice()

  25. roll2 = roll_dice()

  26.  

  27. for j in range(2,13):

  28. if (roll1+roll2) == j:

  29. roll_dict[j] += 1

  30. #遍历列表

  31. for i, result in roll_dict.items():

  32. print('点数{}的次数:{},频率:{}'.format(i, result, result / total_times))

  33. if __name__ == '__main__':

  34. main()

版本3.0

功能:可视化抛掷2个骰子的结果

Python数据可视化
• matplotlib模块

matplotlib是一个数据可视化函数库
• matplotlib的子模块pyplot提供了2D图表制作的基本函数
• 例子:https://matplotlib.org/gallery.html

  1. '''

  2. 功能:模拟掷骰子

  3. 版本:3.0

  4. '''

  5. import random

  6. import matplotlib.pyplot as plt

  7. def roll_dice():

  8. '''

  9. 模拟掷骰子

  10. '''

  11. roll = random.randint(1,6)

  12. return roll

  13.  

  14. def main():

  15. total_times = 100

  16. #初始化列表[0,0,0,0,0,0]

  17. result_list = [0] * 11

  18.  

  19. #初始化点数列表

  20. roll_list = list(range(2,13))

  21.  

  22. roll_dict = dict(zip(roll_list ,result_list )) #元组结构

  23. # 记录骰子的结果

  24. roll1_list = []

  25. roll2_list = []

  26. for i in range(total_times ):

  27. roll1 = roll_dice()

  28. roll2 = roll_dice()

  29. roll1_list.append(roll1)

  30. roll2_list.append(roll2)

  31. for j in range(2,13):

  32. if (roll1+roll2) == j:

  33. roll_dict[j] += 1

  34. #遍历列表

  35. for i, result in roll_dict.items():

  36. print('点数{}的次数:{},频率:{}'.format(i, result, result / total_times))

  37.  

  38. #数据可视化

  39. x = range(1,total_times +1)

  40. plt.scatter (x,roll1_list ,c='red',alpha = 0.5) #alpha:透明度 c:颜色

  41. plt.scatter (x, roll2_list, c='green',alpha=0.5)

  42. plt.show()

  43. if __name__ == '__main__':

  44. main()

 

 版本4.0

功能:对结果进行简单的数据统计和分析

简单的数据统计分析
• matplotlib直方图

  1. '''

  2. 功能:模拟掷骰子

  3. 版本:4.0

  4. '''

  5. import random

  6. import matplotlib.pyplot as plt

  7. # 解决中文显示问题

  8. plt.rcParams['font.sans-serif'] = ['SimHei'] #SimHei黑体

  9. plt.rcParams['axes.unicode_minus'] = False

  10.  

  11. def roll_dice():

  12. '''

  13. 模拟掷骰子

  14. '''

  15. roll = random.randint(1,6)

  16. return roll

  17.  

  18. def main():

  19. total_times = 100

  20. # 记录骰子的结果

  21. roll_list=[]

  22.  

  23. for i in range(total_times ):

  24. roll1 = roll_dice()

  25. roll2 = roll_dice()

  26. roll_list.append(roll1 + roll2)

  27. #数据可视化

  28. plt.hist(roll_list ,bins=range(2,14),normed= 1,edgecolor='black',linewidth=1)

  29. #edgeclor:边缘颜色 linewidth:边缘宽度 normed=1时转化为概率图

  30. plt.title('骰子点数统计') #名称

  31. plt.xlabel('点数')

  32. plt.ylabel('频率')

  33. plt.show()

  34. if __name__ == '__main__':

  35. main()

 

版本5.0 

功能:使用科学计算库简化程序,完善数据可视化结果

使用科学计算库NumPy简化程序

NumPy的操作对象是多维数组ndarray
• ndarray.shape 数组的维度
• 创建数组:np.array(<list>),np.arrange() …
• 改变数组形状 reshape()

NumPy创建随机数组
• np.random.randint(a, b, size)
创建 [a, b)间形状为size的数组

NumPy基本运算
• 以数组为对象进行基本运算,即向量化操作
• 例如:

 

np.histogram() 直接输出直方图统计结果 

 

matplotlib绘图补充
• plt.xticks() 设置x坐标的坐标点位置及标签
• plt.title()设置绘图标题
• plt.xlabel(), plt.ylabel() 设置坐标轴的标签

  1. '''

  2. 功能:模拟掷骰子

  3. 版本:5.0

  4. '''

  5. import random

  6. import matplotlib.pyplot as plt

  7. import numpy as np

  8. # 解决中文显示问题

  9. plt.rcParams['font.sans-serif'] = ['SimHei'] #SimHei黑体

  10. plt.rcParams['axes.unicode_minus'] = False

  11.  

  12. def roll_dice():

  13. '''

  14. 模拟掷骰子

  15. '''

  16. roll = random.randint(1,6)

  17. return roll

  18.  

  19. def main():

  20. total_times = 1000

  21. # 记录骰子的结果

  22. roll1_arr = np.random.randint(1,7,size=total_times)

  23. roll2_arr = np.random.randint(1, 7, size=total_times)

  24. result_arr = roll1_arr + roll2_arr

  25. # hist,bins = np.histogram(result_arr ,bins=range(2,14))

  26. # print(hist)

  27. # print(bins)

  28. #数据可视化

  29. plt.hist(result_arr ,bins=range(2,14),normed= 1,edgecolor='black',linewidth=1,rwidth= 0.8)

  30. #edgeclor:边缘颜色 linewidth:边缘宽度 normed=1时转化为概率图 rwidth:柱子宽度

  31. #设置X轴坐标点

  32. tick_labels = ['2点', '3点', '4点', '5点',

  33. '6点', '7点', '8点', '9点', '10点', '11点', '12点']

  34. tick_pos = np.arange(2, 13) + 0.5

  35. plt.xticks(tick_pos,tick_labels)

  36.  

  37. plt.title('骰子点数统计') #名称

  38. plt.xlabel('点数')

  39. plt.ylabel('频率')

  40. plt.show()

  41. if __name__ == '__main__':

  42. main()

最新毕业设计成品

版权所有© 帮我毕业网 并保留所有权利

QQ 1370405256 微信 biyebang

QQ:629001810微信:biyebang

收缩