I need to generate a uniformly random point within a circle of radius R.
I realize that by just picking a uniformly random angle in the interval [0 ... 2π),
Here is a fast and simple solution.
Pick two random numbers in the range (0, 1), namely a
and b
. If b < a
, swap them. Your point is (b*R*cos(2*pi*a/b), b*R*sin(2*pi*a/b))
.
You can think about this solution as follows. If you took the circle, cut it, then straightened it out, you'd get a right-angled triangle. Scale that triangle down, and you'd have a triangle from (0, 0)
to (1, 0)
to (1, 1)
and back again to (0, 0)
. All of these transformations change the density uniformly. What you've done is uniformly picked a random point in the triangle and reversed the process to get a point in the circle.