在环形内生成一个均匀的随机点(环形) [英] Generate a uniformly random point within an annulus (ring)
问题描述
可能存在重复:
:
Possible Duplicate:
Create random number within an annulusI would like to obtain a uniformly obtained random point within an annulus, that is, the area that lies inside a circle of radius
R1
, but outside a circle of radiusR2
, whereR1 > R2
and both circles are centered at the same point. I would like to avoid using rejection sampling.If possible, I would like the solution to be similar to this one —used for calculating random points within a circle— which I find it extremely elegant and intuitive. That is, I would also like to avoid using the square root.
解决方案EDIT: Please note that this solution might not be uniform. See comments by Mark Dickinson below.
Ok, I think I figured it out. Note that this solution is heavily inspired in this answer, and that r1 = R1/R1 and r2 = R2/R1.
Pseudo-code:
t = 2*pi*random() u = random()+random() r = if u>1 then 2-u else u r = if r<r2 then r2+r*((R1-R2)/R2) else r [r*cos(t), r*sin(t)]
Here it is in Mathematica.
f[] := Block[{u, t, r}, u = Random[] + Random[]; r1 = 1; r2 = 0.3; t = Random[] 2 Pi; r = If[u > 1, 2 - u, u]; r = If[r < r2, r2 + r*((R1 - R2)/R2), r]; {r Cos[t], r Sin[t]}] ListPlot[Table[f[], {10000}], AspectRatio -> Automatic]
What it does is to remap all the numbers falling inside the inner circle into the annulus, spreading them evenly. If somebody finds a problem regarding the uniformity of this solution please comment.
Compare with this other solution found here:
这篇关于在环形内生成一个均匀的随机点(环形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!