找回密码
 快速注册
搜索
查看: 25|回复: 0

Throwing Dice

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2022-8-19 10:39 |阅读模式
Possible outcomes of throwing a single die:
  1. dots = Range[6]
复制代码
We define a uniform probability distribution for the results 1 to 6, and call it "dice":
  1. dice = DiscreteUniformDistribution[{1, 6}];
复制代码
How many times we toss the die => trials:
  1. trials = 120;
复制代码
Toss it:
  1. toss = Table[RandomVariate[dice], trials]
复制代码
{3,3,6,5,2,3,2,5,3,2,1,2,3,6,4,5,1,4,6,4,3,5,4,6,1,2,4,2,4,2,1,2,3,6,5,2,1,1,1,3,4,2,1,4,2,5,1,5,1,4,2,4,2,5,1,2,6,6,2,6,5,4,5,2,6,2,4,3,2,1,4,4,5,5,2,3,3,5,6,2,3,5,6,4,5,4,2,3,2,4,5,1,2,4,2,3,2,1,4,4,2,3,1,1,5,6,1,1,2,4,5,2,1,4,2,2,1,6,2,5}

Count how many of each number (between 1/2 and 3/2, 3/2 and 5/2, etc):
  1. freq = BinCounts[toss, {.5, 6.5, 1}]
复制代码
{20, 31, 15, 22, 19, 13}

Plot the frequency of each result as a histogram:
  1. BarChart[freq]
复制代码
5-Figure3-1.png
Efficient way to add up the elements of the list "dots" ==>  Apply the "Plus" operator to each element:
  1. Plus @@ dots
复制代码
21

Calculate the average value of the number of dots:
  1. N[Plus @@ (freq*dots) / trials]
复制代码

3.23333

Calculate the average value of the number of dots squared:
  1. N[Plus @@ (freq*dots^2) / trials]
复制代码

13.1167

  1. Rolldice[trials_] := (
  2.   dots = Range[6];
  3.   dice = DiscreteUniformDistribution[{1, 6}];
  4.   toss = Table[RandomVariate[dice], trials];
  5.   freq = BinCounts[toss, {.5, 6.5, 1}];
  6.   BarChart[freq];
  7.   avgdots = N[Plus @@ (freq*dots)/trials];
  8.   avgdotssq = N[Plus @@ (freq*dots^2)/trials];
  9.   sdev = Sqrt[avgdotssq - avgdots^2];
  10.   Print["mean = ", avgdots, "  standard deviation = ", sdev])
复制代码

Let's define a function to evaluate mean and standard deviation: 5-Figure3-1.png
Compare with the formulas for discrete uniform distribution:\begin{aligned}\frac{6+1}2&=3.5\\\sqrt{\frac{6^2-1}{12}}&=1.70783\end{aligned}

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 11:58

Powered by Discuz!

× 快速回复 返回顶部 返回列表