[英]Simulating archers hitting a target monte carlo method
3 archers are shooting at a target with probabilities p1, p2, p3. 3名弓箭手以概率p1,p2,p3射击目标。 With a Monte Carlo simulation I am supposed to find an approximation of the probabilities of the following events:
通过蒙特卡洛模拟,我应该找到以下事件的概率的近似值:
This is the first problem of this type that I am approaching. 这是我要解决的第一个问题。 I can solve it easily using probabilities formulas, but I have no idea how to approach the problem using a simulation.
我可以使用概率公式轻松解决它,但是我不知道如何使用模拟方法解决问题。
I would do something like this: 我会做这样的事情:
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;
}
Here, the archer
objects shoot
method will return true or false with their respective probabilities. 在这里,
archer
shoot
对象的方法将以其各自的概率返回true或false。 sample_problem2
will simulate an event you need. sample_problem2
将模拟您需要的事件。 The latter method will take count
samples, and will estimate the probability of the simulated event. 后一种方法将获取
count
样本,并估计模拟事件的概率。
If you have some example code, we can help you develop a solution which fits better to what you already have. 如果您有一些示例代码,我们可以帮助您开发更适合您现有解决方案的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.