Generate a random point within a circle (uniformly)

前端 未结 21 2313
逝去的感伤
逝去的感伤 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:19

    The area element in a circle is dA=rdr*dphi. That extra factor r destroyed your idea to randomly choose a r and phi. While phi is distributed flat, r is not, but flat in 1/r (i.e. you are more likely to hit the boundary than "the bull's eye").

    So to generate points evenly distributed over the circle pick phi from a flat distribution and r from a 1/r distribution.

    Alternatively use the Monte Carlo method proposed by Mehrdad.

    EDIT

    To pick a random r flat in 1/r you could pick a random x from the interval [1/R, infinity] and calculate r=1/x. r is then distributed flat in 1/r.

    To calculate a random phi pick a random x from the interval [0, 1] and calculate phi=2*pi*x.

提交回复
热议问题