Dominic M
Dominic M

Reputation: 11

Probability of getting a particular sum from n m-sided dice c#

I am trying to generate a probability of getting a specific number from n dice, with no guarantee of them having the same number of sides. (eg, 1d6 + 2d10)

I know there is a really expensive way of doing it (With recursion), but if there is a mathematical way of determining the chance of an event happening, that would be way better.

Upvotes: 1

Views: 1197

Answers (3)

btilly
btilly

Reputation: 46455

@Rup already gave one standard solution, the bottom up dynamic programming method.

The top down approach is to write your recursive function..and then memoize it. That is when your function is called you first check whether you have seen this before (ie you look into a dictionary to see if you have a "memo" to yourself about the answer), and if you haven't you calculate the answer and save it. Then you return the memoized answer.

The usual tradeoffs apply:

  1. Top down is easier to figure out and write.
  2. Bottom up lets you see that you don't need to store 2 dice answers when you have the 3 dice ones, and therefore reduces working memory requirements.

Therefore it is good to know both approaches, but I always reach for a top down approach first.

Upvotes: 0

Rup
Rup

Reputation: 34418

One way to do this:

  • Create an output array count with length sum(sides all dice)+1, i.e. so that the max that can possibly be rolled works as an index.
  • This represents the number of ways that the index can be rolled. Initialise this with [0] = 1.
  • For each dice of N sides, enumerate the results of each possible rolled value.
    • Copy the existing count array into prev, say, and create a new empty count array
    • for roll = 1 to N, for total = 0 to count.length-1-roll, count[total+roll]+=prev[total]
  • Now the probability of rolling value = count[value] / sum(count)

Notes:

  • This isn't, as you feared, either really expensive or needs recursion. This will be O(N^2) where N as the total faces on all dice.
  • This will compute the probability of all outputs not just the one output that you're interested in, which may be an issue if the total faces is extremely large and the value you're interested in small. You could cap the count array at length (value you're interested in) + 1, if necessary, and compute the total number of rolls as the product of each die face as you process it rather than from sum(count) as I've suggested above.

Upvotes: 2

LDS
LDS

Reputation: 362

Here I generated from 2 dice rolling

1 Randon() will be generated from n faces

2 here n times is rolled on

3 sum is displayed for n rolled

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dicerolling
{
    class Program
    {
        static void Main(string[] args)
        {

            Random x = new Random();
            int throw_times = 1;
            int sum = 0;
            int[] dice = new int[2];
            dice[0] = x.Next(1, 7);
            dice[1] = x.Next(1, 7);
            Console.WriteLine("enter the no of rollings :");
            var n = int.Parse(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                dice[0] = x.Next(1, 7);
                dice[1] = x.Next(1, 7);

                int total_var = dice[0] + dice[1];
                sum +=  dice[0] + dice[1] ;//total in array

                Console.Write("Throw " + throw_times + ": " + dice[0] + " d " + dice[1] + " = ");
                Console.WriteLine(total_var);
                throw_times++;

                Array.Sort(dice);

                for (int a = dice.Length - 1; a >= 0; a--)
                {
                    int s = dice[a];
                    Console.WriteLine("#" + s);
                }
            }

            Console.WriteLine("Total sum: " + sum);//only returns sum of last 2 rolls


            Console.ReadLine();
        }
    }
}

Upvotes: -1

Related Questions