0

I've been working on a project and I need to be able to return a point in a square that acts kind of like a bulls-eye. I wanted to make it return a point that would follow a Gaussian distribution, meaning after a lot of generated results we would see the majority of the points generated near the center and the number would diminish as it goes towards the outer limits of the square. What would be the best way to go about this?

I have a method as seen here:

    public static Point randomPoint(final Random random, final Point center,
        final double sd) {
        Point p = null;

        return p;
    }

But I am really stuck on where to go from here. With general numbers, a random gaussian number would just use a min and a max with a mean and standard deviation. would I do something similar here but do the min and max for both x and y?

200_success
  • 7,286
  • 1
  • 43
  • 74
Ibrennan208
  • 1,345
  • 3
  • 14
  • 31

1 Answers1

2

To generate a symmetric 2d Gaussian distribution, you actually only have to generate 2 separate Gaussian numbers and take them as x and y, like this:

new Point(center.getX()+random.nextGaussian()*sd, 
          center.getY()+random.nextGaussian()*sd);

Note however that mean (the center) and deviation (I assume 'sd' in your example) does not equal min/max. It basically means that roughly 2/3 of all points will be less then 'sd' far from the center, roughly 95% will be at most '2*sd' far. However there is a non-zero probability for all points, however far.

This means, you might want to 'crop' the points to the rectangle of your interest. Note however, there are two approaches to do this:

  • If a point is outside the rectangle just put it on the border (aka. do min/max checking and take min/max if it's out of bounds)
  • Repeat the generation if point is outside of bounds

The first one will potentially deform your distribution, as it will be more likely that a point is on exactly the border than it should be. The second one costs a bit more processing, however it will preserve the distribution.

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38