# The probability of a sum in the form of cubes with different types of cubes

I am currently working on a Java application where I need to calculate the rolling probabilities of each sum for different dice. The bone types I support are d4 (4-sided dice), d6 (6-sided dice), d8 (8-sided dice), d10, d12, and d20. The user will be able to enter the amount of each bone type they want to use in the calculation. For example, the user can enter 6 d6 and 4 d4.

With this information in mind (the number of cubes of each type), I expect to calculate the probability that each possible amount can be collapsed. I will then use this information to create a chart showing the probability distribution from the selected combination of the provided bones.

This application is written in Java.

Where I am now, I have a function that will calculate the probability of a specific sum to use only one size of dice

```
/*
Recursively calculates the probability of rolling a particular number
when rolling multiple dice of one type
@param dice Number of dice
@param seekedValue Value whose probability is being calculated
@param sides Number of sides on the type of die
*/
private double diceProb(int dice, int seekedValue, int sides){
if (dice == 0){
if (seekedValue == 0){
return 1.0;
} else {
return 0.0;
}
} else {
double sum = 0;
for (int i = seekedValue - sides; i < seekedValue; i++){
sum += diceProb(dice -1, i, sides) / sides;
}
return sum;
}
}
```

Then I use this code to find all possible probabilities

```
/*
Variable Explanations:
diceEntries: This array list contains the number of each dice supplied by the user.
It is ordered by number of sides, with d4 at the beginning and d20 at the end
diceValues: This array contains the sides of the dice types
probArray: this array list will contain the probabilities of each sum possible
min: the minimum sum possible
max: the maximum sum possible
*/
ArrayList<Integer> diceEntries
ArrayList<Float> probArray = new ArrayList<>();
int[] diceValues = {4,6,8,10,12,20};
float prob = 0;
for (int i = min; i <= max; i++){
for (int j = 0; j <= 5; j++) {
prob = (float) diceProb(diceEntries.get(j), i, diceValues[j]);
if (prob != 0) {
probArray.add(prob);
}
}
}
```

My current code is only capable of handling cubes of the same size, i.e. only d6s or d4s, not a combination.

If the community can provide some guidance, it would be greatly appreciated. I am also open to new approaches. For example, I read that function generation might be the best way to do this, but my combinatonic statistics are a bit weak and if anyone has a way to code it would be nice to see that.

Thanks a lot to people

source to share

Another entry for the brute force method, using a list of integers (sides of a bone) to handle multiple types of stamps. The advantage is that if you want multiple probabilities, you can run it once and then just ask for different probabilities. The disadvantage is that the brute force method is very ineffective for obtaining a single probability.

```
public int[] probs;
public void genRolls(int sum, List<Integer> sides)
{
if (sides.size() == 0)
{
probs[sum]++;
return;
}
int top = sides.get(0);
for (int x = 1; x <= top; x++)
genRolls(sum+x, sides.subList(1, sides.size()));
}
public void diceprob(int target, List<Integer> sides)
{
int maxval = 0;
double possibilities = 1;
for (Integer i : sides)
{
maxval+= i;
possibilities *= i;
}
probs = new int[maxval+1];
genRolls(0, sides);
System.out.println("Probability is " + (probs[target]/possibilities));
}
```

source to share

You can just simulate all combinations. The code below does this for `nD6`

, but the example needs to be expanded to include multiple bone types.

```
public class Main {
static final int FACES = 6;
int[] buckets;
int[] currentCombination;
int totalCombinations;
public Main(int numberOfDices) {
this.buckets = new int[FACES * numberOfDices];
this.currentCombination = new int[numberOfDices];
this.totalCombinations = 0;
}
public void calculate() {
calculate(this.currentCombination.length);
for (int i = 0; i < this.buckets.length; ++i) {
System.out.println((i + 1) + ":\t" + this.buckets[i] + " / "
+ this.totalCombinations);
}
}
public void calculate(int dicesLeft) {
if (0 == dicesLeft) {
int sum = 0;
for (int die : this.currentCombination) {
sum += die;
}
++this.buckets[sum - 1];
++this.totalCombinations;
} else {
--dicesLeft;
for (int die = 1; die <= FACES; ++die) {
currentCombination[dicesLeft] = die;
calculate(dicesLeft);
}
}
}
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
Main m = new Main(N);
m.calculate();
}
}
```

source to share

The probability of the sum of the independent variables is the convolution of the probability of each variable. For discrete variables, convolution is a simple summation that can be easily implemented in Java. My advice is to organize the calculation as follows:

```
get numbers of dice and numbers of sides
for each die
construct array with length=number of sides and each element=1/(number of sides)
let convolution result=first array
for second through rest of arrays
let convolution result = convolution(result, next array)
output result
```

The convolution of multiple functions can be computed two at a time; it doesn't matter in what order.

Convolution for two arrays `a`

and is `b`

valid:

```
for i=0 through length(a) + length(b) - 2 inclusive,
conv[i] = sum(a[j]*b[i - j], j, max(0, i - length(b) + 1), min(length(a) -1, i))
```

You can check your results very conveniently with, say, Octave . `conv`

- convolution function in Octave.

source to share