Dice

Author

Eric W. Weisstein
July 27, 2008

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/Dice.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/Dice.html.

©2008 Wolfram Research, Inc. except for portions noted otherwise

Probability

P[p_,n_,s_]:=(1/(s^n))Sum[(((-1))^k)Binomial[n,k]Binomial[p-s k-1,n-1],{k,0,Floor[(p-n)/s]}]

P[p,n,s]

Sum[(((-1))^k)Binomial[n,k]Binomial[p-s k-1,p-s k -n],{k,0,n}]

With[{n=2},FullSimplify[P[p,n,6],n≤p≤6n]]

Factor/@With[{n=3},FullSimplify[P[p,n,6],n≤p≤6n]]

Rolls to first get a six

<<Statistics`DataManipulation`

cnts=Table[Length@NestWhileList[Random[Integer,{1,6}]&,Random[Integer,{1,6}],#!=6&,1],{100000}];//Timing

Plus@@cnts/Length[cnts]//N

bins=BinCounts[cnts,{1,100,1}]

Flatten[Position[bins,_?(#≠0&)]][[-1]]

Histogram[cnts]

n-Sided Dice

Dice[p_,n_,s_]:=Sum[(-1)^k Binomial[n,k] Binomial[p-s k-1,n-1],{k,0,Floor[(p-n)/s]}]/s^n
DiceList[n_,s_]:=Module[{i},Table[Dice[i,n,s],{i,n,n s}]]
DicePlot[n_,s_,opts___]:=BarChart[Table[{Dice[i,n,s],i},{i,n,n s}],opts]

(*Floor[(s+1)n/2]*)
DiceLikeliestRoll[n_Integer?EvenQ,s_Integer]:=1/2 n(s+1)
DiceLikeliestRoll[n_Integer?OddQ,s_Integer?EvenQ]:=1/2(n(s+1)-1)
DiceLikeliestRoll[n_Integer?OddQ,s_Integer?OddQ]:=1/2 n(s+1)

DiceLikeliestRollP[n_,s_]:=Dice[DiceLikeliestRoll[n,s],n,s]

DiceList[1,6]

DiceList[2,6]

DiceList[3,6]

DiceList[4,6]

Table[{DiceList[4,6][[i]]//N,DiceList[4,6][[i]]},{i,Length[DiceList[4,6]]}]

Table[DiceLikeliestRoll[n,6],{n,10}]

Table[DiceLikeliestRollP[n,6],{n,1,10}]

Show[GraphicsArray[Partition[Table[DicePlot[n,6,DisplayFunction->Identity],{n,4}],2]],
  DisplayFunction->$DisplayFunction]