Forgot password?
 Create new account
View 148|Reply 0

Throwing Dice

[Copy link]

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2022-8-19 10:39:07 |Read mode
Possible outcomes of throwing a single die:
  1. dots = Range[6]
Copy the Code
We define a uniform probability distribution for the results 1 to 6, and call it "dice":
  1. dice = DiscreteUniformDistribution[{1, 6}];
Copy the Code
How many times we toss the die => trials:
  1. trials = 120;
Copy the Code
Toss it:
  1. toss = Table[RandomVariate[dice], trials]
Copy the Code
{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}]
Copy the Code
{20, 31, 15, 22, 19, 13}

Plot the frequency of each result as a histogram:
  1. BarChart[freq]
Copy the Code
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
Copy the Code
21

Calculate the average value of the number of dots:
  1. N[Plus @@ (freq*dots) / trials]
Copy the Code

3.23333

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

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])
Copy the Code

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}

手机版Mobile version|Leisure Math Forum

2025-4-20 22:06 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list