# Turning a matrix matrix into a probability table

I want to build a probability table that is able to answer questions such as "if i roll 5, 4 sided dice what is the probability i get at least a 16?"

My current code is:

numberOfdice = 5;
diceSize = 4;
sampleSpace = Tuples[Table[Range[1, diceSize], {numberOfdice}]];
list = Sort[Total[sampleSpace, {2}]];
min = numberOfdice;
max = numberOfdice*diceSize + 1;
counts = BinCounts[list, {min, max, 1}];
Transpose[{Range[min, max - 1], counts}] // MatrixForm


Which gives me a matrix where the left hand column is the sum of the dice, and the right hand column is the count of that total in the sample space.

But im not sure what to do from here, does anyone have any advice?

• To get probabilities you need to divide by the size of the sample space, but you will probably know this. Well, you have now a list, let it be named "tuples" of tuples with the first element the sum of the dice and the second one the probability. Now, you could use e.g."Select" to get all tuples with at least 16: sel= Select[tuples,#[[1]]>=16&] Here the function "#[[1]]>=16&" picks out all tuple with first element >= 16. Then you only need to add up the probabilities: Total[sel[[All,2]]] – Daniel Huber Sep 29 at 11:00

Generating tuples and then machinating over them will get terribly inefficient very quickly (for example, try 10D20 with your code: you'll blow memory before getting anywhere).

Much easier, and efficient, to appeal directly to a probabilistic result.

Here's a function that given the number of dice and number of faces produces a grid with sums and various probabilities.

probgrid[numberOfdice_, diceSize_] :=
Module[{d, face, probs, aprobs},
probs =
CoefficientList[Sum[d^face, {face, diceSize}]^numberOfdice,
d][[numberOfdice + 1 ;;]]/diceSize^numberOfdice;
aprobs = Accumulate@probs;

Grid[Prepend[
Transpose[{Range[numberOfdice, numberOfdice diceSize], probs,
aprobs, 1 - aprobs + probs, Most[Prepend[aprobs, 0]],
1 - aprobs}], {"Sum", "=", "<=", ">=", "<", ">"}],
Frame -> All]];


probgrid[5, 4]

If you want a decimal result instead of exact, just apply N:
N@probgrid[5, 4]