While the answers so far have covered a lot of ground already I have not seen EmpiricalDistribution
. I would like to build upon this observation by providing a couple of general considerations that I have found to be useful when doing statistical experiments using Mathematica. What users of Mathematica may take for granted may surprise newcomers: You can stay very close to the true "programming language" of models which is Mathematics.
So while one may of course work with low level functions like RandomInteger
or Boole
or Tally
or Count
one misses the flexibility and generality of the statistical framework provided by Mathematica which is easily transferable to lots of other cases.
Working with Probabilities and Distributions in Mathematica
I have come to quite like the general way that working with probabilities and distributions is done in Mathematica and whenever possible I try to stay within that framework.
Oversimplifying a bit one might see four sources for a distribution and accordingly separate Mathematica-functions:
- Parametric Distributions corresponding to some idealized stochastic model (e.g.
DiscreteUniformDistribution
)
- Nonparametric Distributions with the most prominent example being that we have a sample from an experiment (e.g.
EmipricalDistribution
)
- Formula Distributions where we use
ProbabilityDistribution
to generate a distribution from a known PDF or CDF
- Derived Distributions where the distribution is the result of some transformation of random numbers whose distributions are given (e.g.
TransformedDistribution
A nice thing to note about (3) is, that for ProbabilityDistribution
proportionality suffices which is nice for Bayesian statistics as Mathematica will take care of normalization if given the option Method -> "Normalize"
).
Advantages of Working with Distributions
If one comes up with a distribution, everything from then on will be standard. Thus we can:
So let us look at the question at hand and see how this works.
Finding a Derived Parametric Distribution for the Sum of Two Dice
Given a standard experiment we can immediately provide a parametric distribution that discribes the true (unbiased) distribution asymptotically:
SeedRandom["REPEATABLE@151108"]; (* make everything repeatable *)
$PlotTheme = "Detailed";
distSingleThrowOneDie = DiscreteUniformDistribution[ {1, 6} ];
From this we can directly derive the parametric distribution for the event "Sum of two dice after a single throw":
distSumTwoDice = TransformedDistribution[
x + y,
{
x \[Distributed] distSingleThrowOneDie,
y \[Distributed] distSingleThrowOneDie
}
];
plotTheoreticalPDF = DiscretePlot[
Evaluate @ PDF[ distSumTwoDice, x ],
{ x, 1, 12 },
ExtentSize -> 0.5,
PlotMarkers -> "Point"
]

We can now nicely use this distribution for the sum of two dice to calculate probabilities:
N @ Probability[ 2 <= x <= 5, x \[Distributed] distSumTwoDice ]
0.277778
Doing Monte Carlo Simulations for Throwing Two Dice
We can use any distribution to sample from it using RandomVariate
. So let us throw two dice one million times:
totalSample = With[
{
sampleSize = 1000000
},
(* model throwing dice, so that each die might be given its own \
distribution if needed *)
Transpose @ {
RandomVariate[ distSingleThrowOneDie, sampleSize ],
RandomVariate[ distSingleThrowOneDie, sampleSize ]
}
];
We can now use this experiment to see how the empirical distribution of the sum of two dice changes with growing sample size:
Manipulate[
Module[
{
partialSample,
distEmpiricalSumOfTwoDice
},
(* take the first n results and calculate the Totals *)
partialSample = Map[Total] @ totalSample[[ ;; n ]];
distEmpiricalSumOfTwoDice = EmpiricalDistribution @ partialSample;
(* compare the plots *)
Show @ {
plotTheoreticalPDF,
DiscretePlot[
Evaluate @ PDF[ distEmpiricalSumOfTwoDice, x ],
{x, 2, 12 },
PlotStyle -> Red,
PlotRange -> {{ 1, 13}, {0, 0.3} },
PlotLegends -> None
]
}
],
{{ n, 10},{ 10, 100, 1000, 10000, 100000, 1000000 }}
]

Tally
is the function you're looking for $\endgroup$Table
statement doesn't seem complete. $\endgroup$