-
Notifications
You must be signed in to change notification settings - Fork 0
/
P102_roll_dice.py
63 lines (46 loc) · 1.59 KB
/
P102_roll_dice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import numpy as np
#import pandas as pd
from matplotlib import pyplot as plt
def random_dice_roll():
return random.randint(1,6)
trials_list=[]
std_deviation_tmp=0.0
num_trials=100
for trial in range(num_trials):
trials_list.append (random_dice_roll())
avg=sum(trials_list)/float(num_trials)
print ('the trail list is shown as below:\n',trials_list)
print ('the avergae dice roll point = ',avg)
for trial in range(num_trials):
std_deviation_tmp+=(trials_list[trial]-avg)**2
std_deviation=(std_deviation_tmp/(num_trials-1))**0.5
print ("\nnote: you shall be careful for the stdandard deviation calculation, bias (/n) or non-bias (/n-1)!!")
print ('my calculated stdandard deviation (non-bias,/n-1) = ',std_deviation)
print ('use numpy lib to calculate standard deviation directly (non-bias,/n-1) = ', np.std(trials_list, ddof=1))
# another version, calculate standard deviation for various data list.
'''
num_trials=range(100,1000,10)
avgs_list=[]
std_dev_list=[]
std_deviation=0.0
for num_trial in num_trials:
trials_list=[]
for trial in range(1,num_trial):
trials_list.append (random_dice_roll())
print(trial)
avg=sum(trials_list)/float(num_trial)
avgs_list.append (avg)
print (trials_list)
for trial in range(1,num_trial):
print(trial, trials_list[trial])
std_deviation+=(trials_list[trial]-avg)**2
std_deviation=std_deviation/(num_trial-1)
std_dev_list.append (std_deviation)
plt.plot(num_trials, avgs)
plt.xlabel('Number of Trials')
plt.ylabel('Average')
plt.show()
'''