Generate Points in a Circle with Uniform Distribution

Qian Chen bio photo By Qian Chen Comment

One naive but wrong idea is to randomly pick the degree and then the radius. We can find out the probability for a point located at in a circle is , where R is the circle radius, which is different from the uniform probability .

Instead, we can regard a circle composed of infinite triangles. Since we can easily generate uniform-distributed points in a triangle, it is easy to find the correct solution for a circle. See this answer for ref.

As a conclusion, the pseudocode of generator is listed as follows:

# assume the circle is centered at (0, 0) with radius 1
from math import pi, sin, cos
from random import random

def generator():
    d = 2*pi*random()
    r = random()+random()
    if r > 1:
    	r = 2-r
    return (r*cos(d), r*sin(d))

for i in range(10):
    print generator()    
    
comments powered by Disqus