0
$\begingroup$

I'm developing a card game where players need to roll a certain number of 6 sided dice and match numbers (or ranges of numbers) on cards. For example a card says the player rolls 5 dice and needs to roll one (6) and one (5 or 6). Another example would be the player rolls 6 dice and needs to get three (3 or 4).

I need to calculate the probability of a successful roll given any number of dice and specifying winning matches. This seems to be made more difficult by the allowing matches to be multiple sides of the dice.

I would like to write a code function so if you pass the number of dice and a list of integers (each integer is the number of sides that would match) then it would return the probability.

$\endgroup$
  • 1
    $\begingroup$ $6$ dice can produce $6^6 = 46656$ equally likely possibilities, which is a small number in computer memory terms, so you could easily generate them all, see how many meet the card requirements and calculate the probability that way $\endgroup$ – Henry Aug 15 '17 at 8:22
  • $\begingroup$ Agree with Henry that with the current computational power, the brute force method should be practical. Just in case you still want to know more on this topic, you can look for the multinomial distriubution. The second example is easy as it just reduced to binomial distribution. For the first one I am not sure if you mean the two cases $(X_5 = 0, X_6 = 2), (X_5 = 1, X_6 = 1)$, in such case you also just need to sum the multinomial pmf. $\endgroup$ – BGM Aug 15 '17 at 9:34
0
$\begingroup$

Maybe you're right; brute force will suffice given the number / sides of the dice will never get too large. Here's the C# code I wrote to solve the problem (will optimize for parallel processors later):

class Program
    {

        static void Main(string[] args)
        {

            // Simple test of one dice, match one side
            Console.WriteLine(GetProbability(new int[] { 1 }));
            // Six dice, match one dice on one side
            Console.WriteLine(GetProbability(new int[] { 1, 6, 6, 6, 6, 6 }));
            // Six dice, match one dice on one side and one dice with two sides
            Console.WriteLine(GetProbability(new int[] { 1, 2, 6, 6, 6, 6 }));

            Console.ReadKey();
        }

        /// <summary>
        /// Accepts an array of matches and returns the porbability of such a throw
        /// </summary>
        /// <param name="matches"></param>
        /// <returns></returns>
        private static double GetProbability(int[] matches)
        {

            int diceSides = 6;
            List<List<int>> combinations = new List<List<int>>();
            GenerateCombinations(ref combinations, new int[matches.Length], diceSides, matches.Length);

            int matchCount = 0;

            foreach (List<int> items in combinations)
            {
                int c = 0;
                for (int i = 0; i < matches.Length; i++)
                {
                    if (items[i] <= matches[i])
                        c++;
                }
                if (c == matches.Length)
                    matchCount++;
            }

            double v1 = matchCount;
            double v2 = Math.Pow(diceSides, matches.Length);

            return v1 / v2;

        }

        /// <summary>
        /// A recursive function to create a combination list of dice values
        /// </summary>
        /// <param name="combinations">A data structure to hold all combinations</param>
        /// <param name="values">A working array with number of elements equal to number of dice thrown</param>
        /// <param name="diceSides">The number of sides on a dice</param>
        /// <param name="depth">The number of dice thrown</param>
        private static void GenerateCombinations(ref List<List<int>> combinations, int[] values, int diceSides, int depth)
        {

            for (int i = 1; i <= diceSides; i++)
            {
                values[depth - 1] = i;

                if (depth == 1)
                {
                    List<int> tList = new List<int>(values);
                    tList.Sort();
                    combinations.Add(tList);
                }
                else
                {
                    GenerateCombinations(ref combinations, values, diceSides, depth - 1);
                }
            }
        }

    }
$\endgroup$

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.