We need to generate a random point in a circle with uniform distribution.

A naive approach with polar coordinates by picking a random angle and a random distance doesn’t give uniform distribution - there are more points close to center and fewer points at the radius. This article has explanation and visualization.

NNN points, left to right: naive approach, correct formula, Monte Carlo (XXX attempts)

Correct formula for polar coordinates

In polar coordinates:

angle = random(0, 2 * PI)
distance = R * sqrt(random(0, 1))

Where R is the radius of the circle. And transformation to Cortesian coordinates:

x = distance * cos(angle)
y = distance * sin(angle)

This article and StackOverflow threads give a mathematically correct explanation.

Monte Carlo (multiple attempts)

Another approach is to generate uniformly distributed random points in the bounding box of the circle and pick the first point that is inside the circle.

With random() being a uniformly random number, the naive approach for a point in a square:

x = random()
y = random()

give uniformly distributed points. And x * x + y * y <= R * R provides an easy test for point being inside the circle.

Obvious drawback - undefined number of attempts, but with uniformly distributed points the average amount of attempt should not be greater than 2 (on average we need 4 / PI attempts).