2
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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]]] $\endgroup$ – Daniel Huber Sep 29 at 11:00
5
$\begingroup$

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]];

Your example:

probgrid[5, 4]

enter image description here

If you want a decimal result instead of exact, just apply N:

N@probgrid[5, 4]

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thank you so much, Im not sure i'll be rolling 10d20 any time soon but I sure am glad to have the option now! This is very pretty, now time to study what you did and see if I can lean anything $\endgroup$ – Wombles Sep 28 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.