确定数量的频率显示在掷骰起来 [英] Determine Frequency of numbers showing up in dice rolls

查看:164
本文介绍了确定数量的频率显示在掷骰起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于比赛我试图确定频率一定#会出现在一个给定的#骰子被推出。我知道......这个问题似乎很奇怪。让我尝试用实数解释。

For a game I'm trying to determine the frequency that a certain # will show up at a given # of dice being rolled. I know... that question seems odd. Let me try to explain it with real numbers.

因此​​,对于1模,频率为每个数将是相同的。 1-6将显示相同的次数。

So, for 1 die, the frequency for each number will be identical. 1-6 will show up equal number of times.

现在的2骰子,事情变得不同。我想象5,6,7将要最常轧制,而在光谱的两端号码将显示较少或根本没有(在1的情况下)。我想知道如何计算这个列表,并显示他们以正确的顺序,从最常见的那么频繁。

Now for 2 dice, things get different. I imagine 5,6,7 are going to be the most frequently rolled, while numbers at both ends of the spectrum will show up less or not at all (in the case of 1). I'd like to know how to calculate this list and show them in the proper order, from most frequent to less frequent.

有什么想法?

@duffymo - 这将是非常好的,虽然有某种算法的拿出吧。如此看来,上述方法将需要大量的手工采摘和放置数字的。如果我的模数是动态的,比如10,这样做,用手将ineffecient和麻烦,我认为。 :)

@duffymo - It would be really nice though to have some sort of an algorithm to come up with it. It seems that the above way is going to require a lot of hand picking and placing of numbers. If my die count is dynamic up to say 10, doing that by hand will be ineffecient and troublesome I think. :)

推荐答案

粗糙的递归方法草案做到这一点:

Rough draft of a recursive way to do it:

public static IEnumerable<KeyValuePair<int, int>> GetFrequenciesByOutcome(int nDice, int nSides)
{
    int maxOutcome = (nDice * nSides);
    Dictionary<int, int> outcomeCounts = new Dictionary<int, int>();
    for(int i = 0; i <= maxOutcome; i++)
        outcomeCounts[i] = 0;

    foreach(int possibleOutcome in GetAllOutcomes(0, nDice, nSides))
        outcomeCounts[possibleOutcome] = outcomeCounts[possibleOutcome] + 1;

    return outcomeCounts.Where(kvp => kvp.Value > 0);
}

private static IEnumerable<int> GetAllOutcomes(int currentTotal, int nDice, int nSides)
{
    if (nDice == 0) yield return currentTotal;
    else
    {
        for (int i = 1; i <= nSides; i++)
            foreach(int outcome in GetAllOutcomes(currentTotal + i, nDice - 1, nSides))
                yield return outcome;
    }
}

除非我错了,这应该吐出举办类似KeyValuePairs [键,频率。

Unless I'm mistaken, this should spit out KeyValuePairs organized like [key, frequency].

修改:仅供参考,运行此之后,它显示了频率GetFrequenciesByOutcome(2,6)是:

EDIT: FYI, after running this, it shows the frequencies for GetFrequenciesByOutcome(2, 6) to be:

2:1

3:2

4:3

5:4

6:5

7:6

8:5

9:4

10:3

11:2

12:1

这篇关于确定数量的频率显示在掷骰起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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