0

3 archers are shooting at a target with probabilities p1, p2, p3. With a Monte Carlo simulation I am supposed to find an approximation of the probabilities of the following events:

  • randomly chosen archer hits the target // already solved
  • the target will be hit if all three shoot at it with a single bullet. // no idea how to approach

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.

Hydroxis
  • 95
  • 1
  • 12

1 Answers1

0

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. sample_problem2 will simulate an event you need. The latter method will take count samples, and will estimate the probability of the simulated event.

If you have some example code, we can help you develop a solution which fits better to what you already have.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • I think I've misunderstood the task. I thought I have only a single sample for the second problem which is why I was so confused :) – Hydroxis Dec 12 '15 at 01:16