不同类型骰子的骰子总和概率 [英] Dice Sum Probability with Different types of Dice

查看:105
本文介绍了不同类型骰子的骰子总和概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on a java application where I need to calculate the probabilities of rolling each sum for a variety of dice. The dice types I am supporting are d4 (4 sided dice), d6 (6 sided dice), d8 (8 sided dice), d10, d12, and d20. The user will be able to input the number of each type of dice they want to use in the calculation. For example, a user may enter 6 d6 and 4 d4.

有了这个给定的信息(每种类型的骰子数量),我正在寻找可以计算每个可能和的概率.然后,我将使用此信息创建一个图表,显示所提供的选定骰子组合的概率分布.

With this given information (the number of dice of each type), I am looking to calculate the probability that each possible sum could be rolled. I will then be using this information to create a chart showing the probability distribution from the selected combination of dice provided.

此应用程序是用 Java 编写的.

This application is being written in Java.

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

Where I am currently at is I have a function that will calculate the probability of a specific sum for a using 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;
        }

    }

然后我使用此代码来查找所有可能的概率

I then use this code to find all the possible probabilites

/*
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,而不是它们的混合.

My current code is only able to handle dice of one size, ie only d6s or d4s and not a mix of them.

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

If the community can provide some guidance, it would be greatly appreciated. I am open to over approaches as well. For example, I have read that generating functions might be a better way of doing this, but my combinatorics statistics are a bit weak and if someone did have a way of coding that up, it would nice to see it.

非常感谢大家

推荐答案

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

Another entry for the brute force method, using a list of Integers(dice sides) to handle multiple die types. The advantage is that if you want a lot of probabilities, you can run it once then just query the various probabilities. The disadvantage is that as a brute force method it's very inefficient for just getting 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));
}

这篇关于不同类型骰子的骰子总和概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆