2017-06-02 258 views
0

我的目標是創建一個骰子滾動模擬器,用戶輸入「擲骰數量」,「雙骰子數量」和試驗次數。我的部分代碼(關於錯誤的下半部分和這是從一個類的例子中使用的),我需要獲得相對頻率和實驗概率的幫助,另外教授指出,爲了得到與他相同的數字,隨機數發生器使用整數237.謝謝擲骰子和計算頻率

import random 

# Sets the number of faces on the dice we are rolling 
# Set to 6 for a 6-sided dice, 20 for a 20-sided dice, etc 
NumberOfFaces = int(input("How many sides?:")) 

face = [] # create a list 
# Set the number of elements in the list 
for x in range(0, NumberOfFaces): 
    face.append(0) 

NumberOfRolls = int(input("How many rolls?:")) 

for y in range(0, NumberOfRolls): 
    # roll the dice with randrange, and then add one to that element of the list 
    face[random.randrange(0, NumberOfFaces)] += 1 

numberOfTrials = int(input('How many trials? Enter:')) 

# print out how many times each face came up 
for z in range(0, NumberOfFaces): 
    frequency = (("%d: %d") % (z+1,face[z])) 
    #print(frequency) 

relativeFrequency = [0, 0] 
probability = [0,0] 
error = [0,0] 
for i in range(2, len(frequency)): 
    relativeFrequency.append(frequency/numberOfTrials) 
    probability.append(min(i-1,13-i)/36) 
    error.append(abs(probability[i]-relativeFrequency[i])) 
# end for 


#print(relativeFrequency) 
#print(probability) 
#print(error) 
print() 


# print results 
f1 = "{0:<10}{1:<22}{2:<22}{3:<22}" 
f2 = 71*"-" 
f3 = "{0:>3}  {1:<22.15f}{2:<22.15f}{3:<.15f}" 
print(f1.format("Sum","Relative Frequency","Probability","Error")) 
print(f2) 
for i in range(2, len(frequency)): 
    print(f3.format(i, relativeFrequency[i], probability[i], error[i])) 
#end for 
print() 

我的期望的輸出

Enter the number of dice: -1 
The number of dice must be at least 1 
Please enter the number of dice: 4 
Enter the number of sides on each die: 1 
The number of sides on each die must be at least 2 
Please enter the number of sides on each die: 7 
Enter the number of trials to perform: -1 
The number of trials must be at least 1 
Please enter the number of trials to perform: 10000 
Sum Frequency Relative Frequency Experimental Probability 
---------------------------------------------------------------------- 
4 6 0.00060 0.06 % 
5 18 0.00180 0.18 % 
6 52 0.00520 0.52 % 
7 83 0.00830 0.83 % 
8 166 0.01660 1.66 % 
9 273 0.02730 2.73 % 
10 346 0.03460 3.46 % 
11 469 0.04690 4.69 % 
12 630 0.06300 6.30 % 
13 738 0.07380 7.38 % 
14 836 0.08360 8.36 % 
15 930 0.09300 9.30 % 
16 930 0.09300 9.30 % 
17 985 0.09850 9.85 % 
18 844 0.08440 8.44 % 
19 737 0.07370 7.37 % 
20 589 0.05890 5.89 % 
21 526 0.05260 5.26 % 
22 326 0.03260 3.26 % 
23 238 0.02380 2.38 % 
24 124 0.01240 1.24 % 
25 86 0.00860 0.86 % 
26 49 0.00490 0.49 % 
27 13 0.00130 0.13 % 
28 6 0.00060 0.06 % 

這裏是將一個鏈接圖片爲它

精確查看所需的輸出

+0

有沒有需要幫助的特定步驟?從你的問題來看,社區可以做些什麼來幫助你。 –

+0

我對得到相對頻率和實驗可能性感到困惑 – ghost25

+0

你有沒有試過的東西?看起來「頻率」是N個骰子加起來成爲某個數字的總次數。然後「相對頻率」是頻率數除以試驗次數。最後,實驗概率似乎是數字乘以100,將其轉換爲百分比。例如,在您的示例數據中,總和「10」被滾動了346次。總卷數346/10000 = .0346。然後將其轉換爲百分比爲.0346 * 100 = 3.46% –

回答

0

您需要了解「預期」和「觀察」之間的區別。如果隨機數發生器被認爲是統一的,我們期望每個骰子都是公平的。假設不止一個死亡和結果增加,預期的分佈是一個鐘形曲線。該公式是由我們的數學表兄弟給出,這裏

https://math.stackexchange.com/questions/1010729/when-rolling-multiple-dice-what-is-the-average-value-if-you-only-keep-the-highe

這是需要一點小技巧比小學數學,但不是所有的複雜。

爲了檢驗試驗是否公平,您需要在觀察結果和預期結果之間進行卡方檢驗。