3 ms·
> I found this neat technique, to find random points in a circle where a random radius r and angle theta are chosen and the x,y points are obtained as x = cente
by Sharlin 6y ago
> I found this neat technique, to find random points in a circle where a random radius r and angle theta are chosen and the x,y points are obtained as x = centerX + r * cos(theta) and y = centerY + r * sin(theta)
This misses the crucial point of the technique described in the linked Stackoverflow answer: that r cannot be a uniformly distributed random value if you want a uniform distribution of points in a circle. Rather you need
r = R * sqrt(random())
where R is the radius of the circle and random() gives a uniform distribution of random numbers between 0 and 1.
- layoutIfNeeded 6y agoOr just do the classic rejection sampling technique: do { x = random(-R, R) y = random(-R, R) } while (x*x + y*y > R*R)
- Sharlin 6y agoWell, that’s what the author says they started with, before replacing it with the non-uniform algorithm.
- blueblisters 6y agoThat seems awfully inefficient.
- enriquto 6y agoNot really, about π/4 = 79% of the times you will have a single iteration. The expected number of iterations is the geometric series with this ratio, which is smaller than 2.
- Sharlin 6y agoIn practice it’s likely faster than the method that uses polar coordinates due to the latter’s reliance on transcendental functions.