#### Archived

This topic is now archived and is closed to further replies.

# graph of distribution of dice rolls

This topic is 5775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am trying to work out the number and sides of dice to use in my RPG for different things, but i want to see the distributions of the rolls so i don''t have to trial-and-error it [as much]... right now i have a little program i wrote, which takes the number of dice and how many sides they have for input, and "rolls" them a bunch of times and draws a distribution graph of it for me. however, this is slow and innaccurate unless i use a huge number of iterations (i use 10000 usually, and even then the graph isn''t as smooth and symmetrical as it should be). is there a formula for this? so i could draw the graphs as they "should be"? thanks in advance. --- krez (krezisback@aol.com)

##### Share on other sites
Maybe the binomial distribution can be used for what you're trying to graph.

Edited by - grady on February 20, 2002 5:51:02 PM

##### Share on other sites
with my google search i found out that that won't work (here), as well as a bunch of pages telling me how to use various dice-rolling software for various statistics classes around the world...
thanks for the attempt though!

EDIT -- wow, i left a quote out of the link and the forum got real thin...

Edited by - krez on February 20, 2002 6:34:42 PM

##### Share on other sites
Krez, you seek the multinomial distribution, since the outcome of rolling n dice is the same outcome as rolling 1 die n times and each roll is mutually exclusive.

Cheers,

Timkin

##### Share on other sites
You can use brute force:

  int Count(int iDice, int iSides, int *piCount){ int iDie[iDice]; int i, iSum; char bDone = 0; for (i = 0; i < iDice; i++) iDie[i] = 1; for (i = 0; i < iDice * iSides; i++) piCount[i] = 0; while (!bDone) { int iSum = 0; for (int i = 0; i < iDice; i++) iSum += iDie[i]; piCount[iSum]++; for (i=0, iDie[0]++; (i < (iDice - 1) && (iDie[i] > iSides); i++) { iDie[i] = 1; iDie[i+1]++; } if (i == (iDice - 1)) bDone = 1; } return(pow(iSides, iDice));}

There might be a few errors, but it at least gives the general idea. The return value is the total number of possible outcomes. It is the same value you would get from summing the piCount array. The probability of a number, i, occuring is piCount/iTotal where iTotal is the value returned by the function. The only reason for actually using a generator is to verify that the numbers generated are actually distributed as they should be.

I assume there are less computationally intensive methods to find the probability of a given sum, but right now I''m at a loss as to what that might be. Since it is semetric you can cut it in half by stopping when the last digit passes the halfway mark. So with a 3d6 you could stop when the 3rd die rolls over to 4.

##### Share on other sites
quote:
Original post by LilBudyWizer
You can use brute force:

...which is exactly what krez is doing LBW! From his original post it appears he is doing a Monte Carlo simulation rather than an ordered search, but either way, it's brutish and forceful and has none of the elegance of solving for the distribution analytically!

quote:
Original post by LilBudyWizer
I assume there are less computationally intensive methods to find the probability of a given sum, but right now I'm at a loss as to what that might be.

As I said, it's the multi-nomial distribution. If you flip a coin many times and want to know the probability distribution over numbers of heads and tail, it's the binomial distribution. For a sampling procedure with exactly k mutually exclusive outcomes (A1,...,Ak), with probabilities p1, ,..., pk respectively, it can be shown that the probability of getting x1 outcomes of type A1, x2 of A2,..., xk of Ak, is given by the multinomial distribution:

f(x1,...,xk) =          n!                  ------------ p1x_1...pkx_k                    x1!...xk!

where p1 + ... + pk = 1 and there are n trials (i.e., you roll n dice). So long as you are careful to renormalise your probabilities of each outcome, you can use dice of differing side numbers in this computation.

Good luck,

Timkin

Edited by - Timkin on February 21, 2002 8:26:51 PM

##### Share on other sites
Well, it seems it would be a bit challenging for him to figure out exactly how he is going to use that to find the probabilities of a given total. It isn''t quite as straight forward as you make it sound since you have to find all the sequences and then calculate the probability of each sequence. With a 10D10 that is at least just around 10^5 instead of 10^10. It is still pretty much a brute force approach since you with a 10D10 there are only 91 totals, but around 10^5 sequences.

I don''t feel up to showing the actual code, but you would generate the sequences by generating all sequences where each number is less than or equal to all the numbers before it. Once you have a given sequence you have to count how many times a given value occurs, i.e. how many dice came up one. Then you plug those counts in for the x''s in the multinomial probability density function. Assuming all your dice have the same number of sides then all the p''s are equal to 1/sides. You have to sum all the dice to figure out what you are calculating the probability of and sum all the probabilities for all the sequences that sum to the same value.

If you do it correctly then when you are done if you sum all the probabilities for all the totals it should equal one. You most likely will not be exactly equal to one due to the number of computations. You should be very close to it though.

##### Share on other sites
... that would be true, if there weren''t this wonderful little field within mathematics called Combinatorics! Using Combinatorics we can determine analytically how many ways there are of getting x1 A1''s, x2 A2''s, ... , xk Ak''s and from this we have the sequences you mentioned.

I''m not going to post the entire solution method here, because then noone would learn anything. You learn by doing.

I will give a hint though... consider the hyper-geometric distribution!

Good luck,

Timkin

##### Share on other sites
You lost me there. You don't need to know how many ways to get x1 A1's and so forth. The multinomial calculates the probability of getting any of them. Getting the xn's is from my view the challenge. Now perhaps combinatoric provides a method for finding the xn's, but you don't say that.

Here is a link that explains how to calculate the probability of a given total. The P(p,n,s) function is a function taking the total of interest, p, the number of dice, n, and the number of sides, s.

Edited by - LilBudyWizer on February 22, 2002 10:14:02 AM

##### Share on other sites
ha ha ha.
thanks yous guys, i really do appreciate it... but apparently i''m not as good at mathematics as i once was (or thought i was?)...
and this is not to say that i am uninterested in learning... but really, to even come close to getting this i''d have to reread a few (several?) of those books i still have from school, and all i really want is a few nice graphs to compare when i''m deciding if some stat should be 3d6 or 6d3.
perhaps some day i will want to become learned in these things, but i do know enough probability to say it isn''t likely
brute force it is i guess... dammit...

--- krez (krezisback@aol.com)

##### Share on other sites
my old book and my calculator had an nPr function, and an nCr function, I forget the actual formula for them a long time ago but I think that is what you want. It seems to be a lost formula now that I search for it.

##### Share on other sites
quote:
Original post by krez
all i really want is a few nice graphs to compare when i''m deciding if some stat should be 3d6 or 6d3.

The lower bound of the score is the first number.
The upper bound of the score is the first number multiplied by the second.
The mean and median averages will be the second number + 1, divided by 2. If the first number is greater than 1, this will also be the modal average.

3d6: min = 3, average = 10.5, max = 18
6d3: min = 6, average = 12, max = 18

Increasing the first number also decreases the variance of the data. This means that you''re less likely to get extreme values. This is because there are more combinations that can achieve the central values than the extreme values. (Example: 2d6... there is only one way to score 2, with a 1 on each die, but there are 6 different ways to score 7... 1+6, 2+5, 3+4, 4+3, 5+2, and 6+1). So increasing the number of dice makes the roll more predictable.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
Well, I''m a little lost by the derivation from the multinomial and if you don''t know the notation then the equation itself can be a bit daunting, but it is fairly simple to implement:

  // calc n!/(k!*(n-k)!), i.e. nCr(n,k)int combin(int n, int k){ int i, l, num = n, den = 1; l = (k > (n - k)) ? (n - k) : k; if (l == 0) { num = 1; den = 1; } else { for (i = 2; i <= l; i++) { num *= n-i+1; den *= i; } } return(num/den);}//calc prob of getting total p from n s-sided dicefloat prob(int p, int n, int s){ int i; int iSign = -1; int count = combin(p - 1, n - 1); if ((p < n) || (p > pow(s,n))) count = 0; else { for (i = 1; i <= (p - n) / s; i++) { count += sign*combin(n,k)*combin(p - s*i - 1, n - 1) sign *= -1; } } return(((float) count)/pow(s,n));}

As before there might be a few errors.

##### Share on other sites
One apology first. In my last post, I made a reference to the hyper-geometric distribution. I was thinking of the wrong combinatoric equation... sorry. The equation I meant was for combinations with repetitions, which is similar to sampling with replacement, but subtely different. One can be derived from the other, but it is not so obvious.

quote:
Original post by LilBudyWizer
You lost me there. You don''t need to know how many ways to get x1 A1''s and so forth. The multinomial calculates the probability of getting any of them. Getting the xn''s is from my view the challenge. Now perhaps combinatoric provides a method for finding the xn''s, but you don''t say that.

Unfortunately, I left out the words that if we know how many different ways you could obtain Ai then you would know the value of xi. I assumed that was obvious. My apologies.

quote:
Original post by LilBudyWizer
Here is a link that explains how to calculate the probability of a given total.

Yes, the above hyperlink to Mathworld shows one method of combining the multinomial distribution with the combinatoric equation for combinations with repetitions , which is the solution path I was suggesting (albeit with an incorrect mathematical reference).

Ultimately, you don''t need graphs krez. What you really need are the first two moments of the distribution so that you can know what the most likely outcome of rolling the dice will be (the maximum likelihood outcome will be the same as the distribution mean since the distribution is symmetric and uni-modal) and an estimate of how widely the die rolls with be distributed about this value (the second moment of the distribution: it''s covariance). These can be computed fairly trivially from a Monte Carlo simulation.

Good luck in what ever you choose to do,

Timkin