0

问题是保龄球系统,问题的措辞是:

应用程序将随机选择被击倒的引脚数,有双倍机会选择数字 7、8、9 和 10。

我实施的方式类似于

所以我想出了这样的事情:

pins = [0,0,0,0,0,0,0,0,0,0] // (1 for pin knocked down / 0 for pin standing)

for(i=0; i<pins.length; i++)

    if(pins[i]==0): //pin isn't already down

        if(i<6) 
            if(random(0~100) < 50):
                pins[i] = 1 //knock down
        else
            if(random(0~100) < 66):
                pins[i] = 1 //knock down


ex: [0,1,0,1,0,0,1,1,1,1] // 6 pins knocked down.

所以我只是对引脚 0-6 和 2/3 对引脚 7-9 做 1/2 的概率,我只是对阵列中的所有元素求和,以知道许多引脚已被丢弃。

但我意识到我从来没有得到一个罢工或零,所以我开始怀疑我是否做错了。

假设所有 pin 的机会均等,我可以只做 pinsDropped = random(0,10) 并且我最终会得到罢工 (10) 9% 的机会和 0 个 pin 掉落。但是我有 4 个引脚有双倍的机会被丢弃,我应该更频繁地获得罢工。

我做错了什么?


更新:

是的,我很讨厌概率。与其他人交谈后,有人指出我也很讨厌阅读问题的措辞。我完全误解了这个问题。当它说randomly choose the number of pins knocked downdouble the chance to numbers 7,8,9,10

这意味着我更有可能击倒 10 个引脚而不是 6 个引脚。

7针、8针、9针和10针。不是引脚 7、引脚 8、引脚 9 和引脚 10。

我对自己的愚蠢感到非常沮丧。

所以我把这件事复杂化了很多,

function try(pins, prob) {
    if (prob > pins)
        prob = pins;
    aleat = mt_rand(0, pins+prob);
    if (aleat > pins)
        aleat -= prob;
    return aleat;
}
4

2 回答 2

1

应用程序将随机选择被击倒的引脚数,引脚 7、8、9 和 10 有双倍的机会。

问题描述:

  • 我们总共有 10 个引脚。

  • 引脚 7、8、9 和 10 有双倍机会。

  • 我们总共有 (6 * 1) + (4 * 2) = 6 + 8 = 14 次机会。

  • A = { 1, 2, 3, 4, 5, 6 } ; a 在 A 中

  • B = { 7, 8, 9, 10 } ; b 在 B 中

  • P(a) = 1/14

  • P(b) = 2P(a) = 2/14 = 1/7

建议的解决方案(Java):

代码:

ArrayList<byte> pins = new ArrayList<byte>();
pins.addAll({1,2,3,4,5,6,7,8,9,10});

while (pins.size() > 0)
{
    /** pre-trial **/

    PinsExperiment.trial(pins);

    /** post-trial **/
}

课程:

class PinsExperiment
{
    public static byte getChances(ArrayList<byte> pins)
    {
        byte c = 0;

        for (byte b : pins)
        {
            if (b <= 6) c += 1;
            else c += 2;
        }

        return c;
    }

    public static void trial(ArrayList<byte> pins)
    {
        byte chances = getChances(pins);

        byte r = Math.ceil(Math.random(0,chances));

        byte n = 0;

        for (byte b : pins) if (b <= 6) n++;

        if (r <= n) pins.remove(pins.get(r-1));
        else pins.remove(pins.get(((r-n)/2)-1)+n);
    }
}
于 2013-03-22T06:11:26.123 回答
0

简单的回答:

def randomChoose():
    x = randint(1,14);
    if x<=7:
        return x;
    else:
        return 6+ (x-5)/2;
于 2013-03-22T16:00:01.370 回答