我已完成此代码,但我需要帮助理解为什么输出与所需的不同。这是我的代码。我的教授也说要使用237的种子。
Passenger
这是我输出的3个骰子,6个边,10,000个试验
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
这是所需的(相同的输出):
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 %
答案 0 :(得分:0)
这是一个重组版本:
from random import Random
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
pass
class Dice:
def __init__(self, num_dice, num_faces, seed=None):
self.num_dice = num_dice
self.num_faces = num_faces
self.rng = Random()
if seed is not None:
self.rng.seed(seed)
def roll(self):
return sum(self.rng.randint(1, self.num_faces) for _ in range(self.num_dice))
def histogram(self, num_trials):
hist = [0] * (self.num_dice * self.num_faces + 1)
for _ in range(num_trials):
hist[self.roll()] += 1
return hist
def main():
num_dice = get_int("How many dice? ")
num_faces = get_int("How many sides on each die? ")
num_trials = get_int("How many trials? ")
dice = Dice(num_dice, num_faces, 237)
hist = dice.histogram(num_trials)
print("Sum Frequency Relative Frequency Experimental Probability")
print("-" * 70)
lo = num_dice
hi = num_dice * num_faces
for n in range(lo, hi + 1):
freq = hist[n]
rfreq = freq / num_trials
print("{:d} {:d} {:0.5f} {:0.2f} %".format(n, freq, rfreq, 100. * rfreq))
if __name__ == "__main__":
main()