Generating a random point within a circle (uniformly)

For a circle of radius R you do:

double a = rand() * 2 * PI
double r = R * sqrt(rand())

double x = r * cos(a)
double y = r * sin(a)

where rand() gives a uniformly random number between 0 and 1.

Example output

Here are 4000 points within a radius of 100 pixels generated with the above method.

Comments