编写一个名为
distribution_of_rolls
的函数,该函数需要一个数字 - 滚动两个骰子的次数 - 并以下图所示的形式打印这些卷的值的分布 例如:Distribution of dice rolls 2: 7 ( 3.5%) ******* 3: 14 ( 7.0%) ************** 4: 15 ( 7.5%) *************** 5: 19 ( 9.5%) ******************* 6: 24 (12.0%) ************************ 7: 35 (17.5%) *********************************** 8: 24 (12.0%) ************************ 9: 28 (14.0%) **************************** 10: 18 ( 9.0%) ****************** 11: 9 ( 4.5%) ********* 12: 7 ( 3.5%) ******* ----------------------------- 200 rolls
我的代码在某处错了。它无法打印出来。
def roll():
return randrange(1,7) + randrange(1,7)
def distribution_of_rolls(n:int):
result=({i,0} for i in range(2,13,1))
c=''
result=list(result)
for i in range(n):
a = roll()
print(a)
result[a]= result[a] + 1
print(result)
for i in range(2,13,1):
b=(result[i]/float(n)) * 100
for i in range(int(math.floor(n))):
c = c + '*'
d = "{0:0.1f}%".format(b)
print("{0:2d}: {1:5d} ({2:s}) {3:s}".format(i, result[i], d, c)
print("-------------------------")
print("{0:10d} rolls".format(n))
distribution_of_rolls(20)
答案 0 :(得分:1)
在概率模型中,您可以使用特征函数来计算独立定律的总和。即使有50个骰子,也可以在我的计算机上使用OpenTurns平台即时获得。
import openturns as ot
d = 2 #number of dice
#define dice distribution with possible values: 1, 2, 3, 4, 5, 6
dice_distribution = ot.UserDefined([[i] for i in range(1,7)])
# create same distribution d times then sum
sum_distribution = sum([dice_distribution] * d)
# create a sample of size 200
Sample = sum_distribution.getSample(200)
如果您想计算每个实现:
import numpy as np
unique, counts = np.unique(Sample, return_counts = True)
print(unique)
print(counts/200) # frequency
>>>[ 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.]
[0.05 0.05 0.09 0.115 0.13 0.175 0.125 0.115 0.07 0.04 0.04 ]
您还可以使用OpenTurns查看器绘制概率密度函数(PDF):
import openturns.viewer as otv
graph = sum_distribution.drawPDF()
otv.View(graph)
您还可以检索值:
print(sum_distribution)
>>> UserDefined(
{x = [2], p = 0.0277778},
{x = [3], p = 0.0555556},
{x = [4], p = 0.0833333},
{x = [5], p = 0.111111},
{x = [6], p = 0.138889},
{x = [7], p = 0.166667},
{x = [8], p = 0.138889},
{x = [9], p = 0.111111},
{x = [10], p = 0.0833333},
{x = [11], p = 0.0555556},
{x = [12], p = 0.0277778}
)
答案 1 :(得分:0)
使用更多功能,以便更容易调试。如,
def roll():
return random.randrange(1,7) + random.randrange(1,7)
def create_data(nrolls):
data = collections.defaultdict(int)
for _ in range(nrolls):
data[roll()] += 1
return data
fmtstart = "{dsum:>2}:{count:>6} ({pct:4.1f}%)"
def format_data(data):
result = []
total = float( sum(data.values()) )
tuples = sorted( data.items() )
for k,v in tuples:
start = fmtstart.format(dsum=k,count=v,pct=100*v/total)
end = " " + "*"*v
result.append(start + end)
return "\n".join(result)
print( format_data( create_data(200) ) )
答案 2 :(得分:0)
from random import randrange
def roll():
return randrange(1,7) + randrange(1,7)
def distribution_of_rolls(n:int):
results = {i:0 for i in range(2,13)}
for i in range(n):
results[roll()] += 1
for i in range(2,13):
occurrences = results[i]
percentage = (occurrences/float(n)) * 100
bar = occurrences * "*"
print("{0:2d}: {1:5d} ({2:4.1f}%) {3:s}".format(i, occurrences, percentage, bar))
print("-------------------------")
print("{0:9d} rolls".format(n))
distribution_of_rolls(200)