Re: Gaussian Distribution Using Dice

new topic     » goto parent     » topic index » view thread      » older message » newer message

cklester wrote:

> This looks good, but, again, I don't understand the last math part...
>
>    http://mathforum.org/library/drmath/view/52207.html

This is not a Gaussian Distribution, BTW. A Gaussian Distribution is a
continuous distribution, whereas this is a discrete distribution.
However, the probabilities of sums on multiple dice *approach* a
Gaussian distribution as the number of dice is increased.

> I'd just like a FAST function like this:
>
>    aNumber = 13 -- what's the odds of the sum of 13
>    sides = 6 -- appearing when 6-sided dice
>    rolls = 4 -- are rolled 4 times and added together?
>
>    xFromY = instances( aNumber, sides, rolls )

[big snip]

On the URL mentioned above, the formula published 1937 by Uspensky is
provided. The formula uses the Binomial Coefficient (named C() there),
so we first need a function to calculate this. The Binomial Coefficient
is of fundamental meaning for combinatorial calculations, so it's
useful to have that function anyway.

global type nonnegative_int (object x)
   if integer(x) then
      return x >= 0
   end if
   return 0
end type

global type positive_int (object x)
   if integer(x) then
      return x > 0
   end if
   return 0
end type

global function binomial (integer n, nonnegative_int k)
   -- Binomial Coefficient ("n choose k"):
   -- Number of (unordered) subsets of k elements from a set of n elements.
   -- in : k: nonnegative integer
   --      n: integer >= k
   -- out: positive integer (not including 0)
   atom ret
   integer start

   if n < k then
      return -1              -- Illegal function call
   end if

   start = n - k
   if k > start then         -- swap k,start (so k is max.=floor(n/2))
      start = k
      k = n - k
   end if
   ret = 1
   for i = 1 to k do
      start += 1
      ret = ret*start/i
   end for

   return ret
end function

global function ways_to_roll_given_sum (positive_int x, positive_int n, integer
s)
   -- in : x: number of sides of each die
   --      n: number of dice
   --      s: given sum
   -- out: number of different ways to roll given sum with given dice
   integer ret

   ret = 0
   for k = 0 to floor((s-n)/x) do
      ret += power(-1,k) * binomial(n,k) * binomial(s-x*k-1,n-1)
   end for
   return ret
end function


-- Demo
integer Sides, Dice, Sum, NumberOfWays
atom Probability

Sides = 6
Dice = 4
Sum = 13

NumberOfWays = ways_to_roll_given_sum(Sides,Dice,Sum)
Probability  = NumberOfWays / power(Sides,Dice)
printf(1, "Number of ways(%d,%d,%d) = %d\n",   {Sides,Dice,Sum,NumberOfWays})
printf(1, "Probability   (%d,%d,%d) = %.2f\n", {Sides,Dice,Sum,Probability})



When you want to calculate the number of ways or probabilities for only
a few cases, this code might be faster than the code provided by Lewis.
Please check yourself.

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu