【发布时间】:2017-11-06 11:46:29
【问题描述】:
我已经完成了这段代码,但我需要帮助理解为什么输出与期望的不同。这是我的代码。我的教授还说要使用 237 的种子。
import random
rng = random.Random()
rng.seed(237)
NumberOfRolls = int(input("How many dice?:"))
NumberOfFaces = int(input("How many sides of each dice?:"))
numberOfTrials = int(input('How many trials? Enter:'))
# Set the number of elements in the list
#def throwDice():
def throwDice(NumberOfRolls):
return random.randrange(1,NumberOfFaces)
def rollAllDice(NumberOfRolls):
sum = 0;
for i in range(NumberOfRolls):
v = throwDice(NumberOfRolls);
sum = sum + v;
# print(v);
return sum;
# perform simulation, record and print frequencies
index = (NumberOfRolls*NumberOfFaces)+1;
frequency = [0]*index
rel_frequency_list = [0]*index
exp_prob_list =[0]*index
for i in range(numberOfTrials):
ind = rollAllDice(NumberOfRolls);
frequency[ind] = frequency[ind]+1;
# end for
print()
#print("Frequencies:")
# calculate relative frequencies, probabilities and errors
relativeFrequency = [0, 0]
probability = [0,0]
error = [0,0]
for i in range(2, len(frequency)):
rel_frequency_list[i] = frequency[i]/numberOfTrials;
exp_prob_list[i] = frequency[i]/100;
#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","Frequency","Relative Frequency","Experimental Probability"))
print(f2)
for i in range(NumberOfRolls, len(frequency)):
print(f3.format(i, frequency[i], rel_frequency_list[i], exp_prob_list[i]),"%")
#end for
这是我的 3 个骰子、6 个面、10,000 次试验的输出
Sum Frequency Relative Frequency Experimental Probability
-----------------------------------------------------------------------
3 75.000000000000000 0.007500000000000 0.750000000000000 %
4 232.000000000000000 0.023200000000000 2.320000000000000 %
5 454.000000000000000 0.045400000000000 4.540000000000000 %
6 831.000000000000000 0.083100000000000 8.310000000000000 %
7 1214.000000000000000 0.121400000000000 12.140000000000001 %
8 1468.000000000000000 0.146800000000000 14.680000000000000 %
9 1491.000000000000000 0.149100000000000 14.910000000000000 %
10 1445.000000000000000 0.144500000000000 14.449999999999999 %
11 1157.000000000000000 0.115700000000000 11.570000000000000 %
12 828.000000000000000 0.082800000000000 8.279999999999999 %
13 476.000000000000000 0.047600000000000 4.760000000000000 %
14 230.000000000000000 0.023000000000000 2.300000000000000 %
15 99.000000000000000 0.009900000000000 0.990000000000000 %
16 0.000000000000000 0.000000000000000 0.000000000000000 %
17 0.000000000000000 0.000000000000000 0.000000000000000 %
18 0.000000000000000 0.000000000000000 0.000000000000000 %
这是所需的(相同的输出):
Sum Frequency Relative Frequency Experimental Probability
----------------------------------------------------------------------
3 45 0.00450 0.45 %
4 126 0.01260 1.26 %
5 281 0.02810 2.81 %
6 494 0.04940 4.94 %
7 677 0.06770 6.77 %
8 968 0.09680 9.68 %
9 1191 0.11910 11.91 %
10 1257 0.12570 12.57 %
11 1257 0.12570 12.57 %
12 1164 0.11640 11.64 %
13 932 0.09320 9.32 %
14 683 0.06830 6.83 %
15 469 0.04690 4.69 %
16 282 0.02820 2.82 %
17 122 0.01220 1.22 %
18 52 0.00520 0.52 %
【问题讨论】:
-
random.randrange()的上限是独占的。尝试使用NumberOfFaces + 1。 -
或试试
random.randint()更简单:) -
你的种子也没有像你想象的那样做。使用
random.seed(237),然后使用random.randrange()或randint()。现在你正在设置一个新的 rng 的种子并且从不使用它。 -
使用 randint 产生更接近和更好的结果。如何使用种子?
-
random.seed(237)当您调用 random.randint() 它使用内部 rng。您创建了一个新的,但从未使用它 - rng.randint() 会使用它。