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()