不同类型骰子的骰子总和概率

Dice Sum Probability with Different types of Dice

我目前正在开发一个 java 应用程序,我需要计算掷出各种骰子的每个总和的概率。我支持的骰子类型是 d4(4 面骰子)、d6(6 面骰子)、d8(8 面骰子)、d10、d12 和 d20。用户将能够输入他们想要在计算中使用的每种骰子的数量。例如,用户可以输入 6 d6 和 4 d4。

根据给定的信息(每种类型的骰子数量),我希望计算出每种可能的总和的概率。然后,我将使用此信息创建一个图表,显示所提供的所选骰子组合的概率分布。

此应用程序正在 Java 中编写。

我目前所处的位置是我有一个函数可以计算仅使用一种骰子大小的特定总和的概率

/*
    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;
        }

    }

然后我使用这段代码找出所有可能的概率

/*
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);
        }
    }
}

我当前的代码只能处理一种尺寸的骰子,即只能处理 d6s 或 d4s,不能混合使用它们。

如果社区能提供一些指导,将不胜感激。我也对过度方法持开放态度。例如,我读过生成函数可能是更好的方法,但我的组合统计有点薄弱,如果有人确实有一种编码方法,我很高兴看到它。

非常感谢大家

您可以简单地模拟所有组合。下面的代码针对 nD6 执行此操作,但该示例应可扩展到多种骰子类型。

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();
    }
}

蛮力法的另一个条目,使用整数列表(骰子边)来处理多种骰子类型。好处是如果你想要很多概率,你可以运行一次,然后查询各种概率。缺点是作为一种蛮力方法,只得到一个概率是非常低效的。

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));
}

自变量之和的概率是每个变量概率的卷积。对于离散变量,卷积是一个简单的求和,您可以在 Java 中轻松实现。我的建议是这样组织计算:

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

几个函数的卷积一次可以计算两个;不管顺序如何。

两个数组 ab 的卷积只是:

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))

您可以使用 Octave 非常方便地检查您的结果。 conv是Octave中的卷积函数。