简体   繁体   English

找到骰子的分布

[英]find the distribution of dice rolls

Write a function called distribution_of_rolls that takes one number — the number of times to roll two dice — and prints the distribution of values of those rolls in the form shown below.编写一个名为distribution_of_rolls的函数,它接受一个数字——掷两个骰子的次数——并以如下所示的形式打印这些掷骰子的值的分布。
for example:例如:

 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

My code somewhere is wrong.我的代码在某处是错误的。 It can not print out.它无法打印出来。

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)

In probabilitic modelling, you can compute the sum of independant laws using caracteristic functions.在概率建模中,您可以使用特征函数计算独立定律的总和。 Even with 50 dice, it's instantaneous on my computer using the OpenTurns platform.即使有 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) 

If you want to count each realisation:如果要计算每个实现:

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 ]

You can also draw the probability density function (PDF) using OpenTurns viewer:您还可以使用 OpenTurns 查看器绘制概率密度函数 (PDF):

import openturns.viewer as otv 
graph = sum_distribution.drawPDF()
otv.View(graph)

概率分布函数的渲染图

You can also retrieve the values:您还可以检索值:

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}
   )

With d = 50 dice, the density will be more like a Gaussian distribution:当 d = 50 个骰子时,密度将更像高斯分布: 在此处输入图片说明

Use more functions so that it becomes easier to debug.使用更多的功能,让调试变得更容易。 Eg,例如,

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) ) )
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM