mardi 1 août 2017

Random point in circle method is not uniformly distributed

I have the following method in Java:

public static Vector2d random(Circle circle) {
    // this returns a random number between 0 and Math.PI * 2
    double angle = MathUtils.random(0, Math.PI * 2);
    // give the point inside the unit circle
    // this returns a normalized vector from a given angle
    Vector2d point = new Vector2d(angle);
    // however, this is only along the edge
    // now add a random magnitude (because this is a normalized vector, we can just multiply it by the desired magnitude)
    double magnitude = Math.random();
    point = point.multiply(magnitude);
    // now expand this to fit the radius
    point = point.multiply(circle.getRadius());
    // now translate by circleCenter
    return point.add(circle.getCenter());
}

This does return a point in the defined circle, however, when you do this many times and plot the points, you can clearly see most points will be toward the center.

Why is this? I don't see how my math can do this.

Comment if you want me to add an image of the points on the plot, if you think that could be helpful.




Aucun commentaire:

Enregistrer un commentaire