Finding a random position in the unit circle

1.4k Views Asked by At

Consider the unit circle (with radius $1$, because I can rescale it). Now, choose a random point inside it. How do you do this? The method I'm using is by choosing a random direction, from $0$ to $360$, and then a 'magnitude' variable, from $0$ to $1$. Then, I set the x to the cosine (and the y to the sine), of this direction, multiplied by the magnitude. But this means that, if we consider a ring in this circle representing all points with a magnitude from $0$ to $0.1$, it would have the same likelihood of being selected as the ring with the magnitudes from $0.9$ to $1$, despite $0$-to-$0.1$ having less area. I know that the likelihood of a ring of a single magnitude being selected should be proportional to the magnitude, but how do we express this within an equation?

3

There are 3 best solutions below

2
On BEST ANSWER

Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers

1
On

Let $R$ and $\Phi$ be independent random variables

Let $\Phi$ have uniform distribution on $[0,2\pi)$ or - if you want degrees - on $[0,360)$.

Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=\frac{\pi r^2}{\pi1^2}=r^2$ for $r\in[0,1]$ and consequently PDF $f_R(r)=2r$ if $r\in[0,1]$ and $0$ otherwise.

Then $(R\cos\Phi,R\sin\Phi)$ has uniform distribution on disk $\{x\in\mathbb R^2\mid ||x||\leq1\}$.

The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk $\{x\in\mathbb R^2\mid ||x||\leq r\}$ divided by the area of whole disk $\{x\in\mathbb R^2\mid ||x||\leq 1\}$.

0
On

If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $\theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(r\cos\theta, r\sin\theta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $\operatorname{f}$, is the inverse of the primitive (indefinite integral) of $\operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.

The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $\Theta$, $\Theta r.dr$.

But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2\leq1$, & if it fails discard it: though you are discarding a propotion $1-\pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.