模拟弓箭手击中目标monte carlo方法

Simulating archers hitting a target monte carlo method

3 名弓箭手以 p1、p2、p3 的概率射向一个目标。通过 Monte Carlo 模拟,我应该找到以下事件概率的近似值:

这是我处理的第一个此类问题。我可以使用概率公式轻松解决它,但我不知道如何使用模拟来解决这个问题。

我会这样做:

public class Archer {
    private Random random = new Random();
    public final double chance;
    public Archer(double chance) {
        this.chance = chance;
    }
    public boolean shoot() {
        return random.nextDouble() < chance;
    }
}

// ...

public boolean sample_problem2() {
    for (Archer archer : archers) {
        if (archer.shoot()) return true;
    }
    return false;
}

public double estimate_sample2(long count) {
    long positive = 0;
    for (long i = 0; i < count; i++) {
        if (sample_problem2()) positive++;
    }
    return (double)positive / (double)count;
}

在这里,archer 对象 shoot 方法将 return 为真或为假的概率与它们各自的概率。 sample_problem2 将模拟您需要的事件。后一种方法将采用 count 个样本,并将估计模拟事件的概率。

如果您有一些示例代码,我们可以帮助您开发一个更适合您已有的解决方案。