i need generate uniformly random point within circle of radius r.
i realize picking uniformly random angle in interval [0 ... 2π), , uniformly random radius in interval (0 ... r) end more points towards center, since 2 given radii, points in smaller radius closer each other points in larger radius.
i found blog entry on over here don't understand reasoning. suppose correct, i understand gets (2/r2)×r , how derives final solution.
regarding rejection sampling: generate random point within r×r square on , on again until 1 within circle. approach has obvious draw-back doesn't provide guarantee termination (even though highly unlikely goes on long).
let's approach archimedes have.
how can generate point uniformly in triangle abc, |ab|=|bc|? let's make easier extending parallelogram abcd. it's easy generate points uniformly in abcd. uniformly pick random point x on ab , y on bc , choose z such xbyz parallelogram. uniformly chosen point in original triangle fold points appear in adc down abc along ac.
now consider circle. in limit can think of infinitely many isoceles triangles abc b @ origin , , c on circumference vanishingly close each other. can pick 1 of these triangles picking angle theta. need generate distance center picking point in sliver abc. again, extend abcd, d twice radius circle center.
picking random point in abcd easy using above method. pick random point on ab. uniformly pick random point on bc. ie. pick pair of random numbers x , y uniformly on [0,r] giving distances center. our triangle thin sliver ab , bc parallel. point z distance x+y origin. if x+y>r fold down.
here's complete algorithm r=1. hope agree it's pretty simple. uses trig, can give guarantee on how long it'll take, , how many random()
calls needs, unlike rejection sampling.
t = 2*pi*random() u = random()+random() r = if u>1 2-u else u [r*cos(t), r*sin(t)]
here in mathematica.
f[] := block[{u, t, r}, u = random[] + random[]; t = random[] 2 pi; r = if[u > 1, 2 - u, u]; {r cos[t], r sin[t]} ] listplot[table[f[], {10000}], aspectratio -> automatic]
Comments
Post a Comment