Generate a random point within a circle (uniformly)

前端 未结 21 2370
逝去的感伤
逝去的感伤 2020-11-22 15:45

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π),

21条回答
  •  悲哀的现实
    2020-11-22 16:22

    You can also use your intuition.

    The area of a circle is pi*r^2

    For r=1

    This give us an area of pi. Let us assume that we have some kind of function fthat would uniformly distrubute N=10 points inside a circle. The ratio here is 10 / pi

    Now we double the area and the number of points

    For r=2 and N=20

    This gives an area of 4pi and the ratio is now 20/4pi or 10/2pi. The ratio will get smaller and smaller the bigger the radius is, because its growth is quadratic and the N scales linearly.

    To fix this we can just say

    x = r^2
    sqrt(x) = r
    

    If you would generate a vector in polar coordinates like this

    length = random_0_1();
    angle = random_0_2pi();
    

    More points would land around the center.

    length = sqrt(random_0_1());
    angle = random_0_2pi();
    

    length is not uniformly distributed anymore, but the vector will now be uniformly distributed.

提交回复
热议问题