I know that by just using a random angle and a random radius within the bounds of your circle, you will end up with points near the center of a circle. Whereas if you do $\sqrt{Random(0,1)}*MaxRadius$ for your radius, you will end up with what appears to be a uniformly random point. I am happy this works but I would like to understand where the square root comes from. The Square Root function in this calculation seems magical to me and I would like to know what it means in this context.
Random Uniformly Distributed Points in a Circle
10k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 3 best solutions below
On
You wish to uniformly distribute points around a disc of radius $R_{\max}$ and centre $\langle 0, 0\rangle$.
As noted, naïvely choosing $\Theta\sim\mathcal U(-\pi;\pi]$ and $R\sim\mathcal U[0;R_{\max}]$ as the distribution of polar coordinates will result in a Cartesian point distribution that is too dense near the centre and too disperse near the rim. In fact a Cartesian point's probability density will be inversely proportional to its radial distance. So we must compensate for this.
We can do this by choosing $R$ using triangular distribution: $R\sim\mathcal T(0,R_{\max},R_{\max})$, which had density $f_R(r) = 2r/R^2_{\max}$ for $r\in[0;R_{\max}]$. Thereby compensating.
A way to generate random numbers for this distribution is to choose a uniformly distributed variable and take the square root of the results. That is: Let $S\sim\mathcal U[0;R_\max^2]$ and set $R=\sqrt{S\,}$.
By a change of variables (chain rule) we can show that gives $R$ the required distribution. $$\begin{align}f_S(s) & = 1/R^2_\max\\ f_R(r) & = f_S(r^2)\left\lvert\dfrac{\mathrm d r^2}{\mathrm d r}\right\rvert \\ & = 2 r/R^2_\max \end{align}$$
Thus a uniform distribution of points in a disc has polar coordinates distributed as $\Theta\sim\mathcal U(-\pi;\pi], \underbrace{R^2}_{S}\sim\mathcal U(0;R_\max^2)$
Which is generated by your code $\rm Let\; Angle = (Random(0,1)*2-1)*Pi\\Let\; Radius = Sqrt(Random(0,1)) * MaxRadius\\ Let\; XOrdinate = Radius*\cos(Angle)\\Let\; YOrdinate=Radius*\sin(Angle)$
On
What we are looking for is a function $r = f(u)$ that takes as input the random variable $u$ with a uniform distribution over $[0,1]$ and outputs a random radius in the range $[0, R]$, where $R$ is the radius of the circle. To be a valid function it must be one-to-one (i.e., $f(x) = f(y) \implies x = y$). It would make no sense to have two values of $u$ that resulted in the same $r$ since that $r$ would be over-weighted compared to other values. Also, $f(0) = 0$ seems like a natural fit. We could also used $f(0) = R$, but that would be more complex for no gain.
To start, consider the probability of picking a $u$ in the range $[u_1, u_2]$ where $u$, $u_1,$ and $u_2$ are all in $[0, 1].$ We can write this as $$P(u_1 \le u \le u_2).$$ Now, let's apply $f$ to everything inside the parentheses: $$P(f(u_1) \le f(u) \le f(u_2)) = P(r_1 \le r \le r_2).$$ I argue that $$P(u_1 \le u \le u_2) = P(r_1 \le r \le r_2).$$ Why? Because $f$ being one-to-one means that if $u$ is in the given range then $r$ is in the given range. All $f$ does is relabel points on a unit line to points on a radius. Probability is preserved.
To cut down on the number of variables, let's set $u_1 = 0,$ which means that $r_1 = f(0) = 0.$ This gives us $$P(0 \le u \le u_2) = P(0 \le r \le r_2).$$ Now, what is $P(0 \le u \le u_2)$? Just $u_2$. The acceptable range is $[0, u_2]$ out of a total range of $[0, 1]$. The probability is then $$P(0 \le u \le u_2) = (u_2 - 0)/(1 - 0) = u_2.$$
What is $P(0 \le r \le r_2)?$ Since we want points to be chosen uniformly over the circle's area, the probability of picking a point within a given region of the circle must be proportional to that regions area. The region defined by $r \le r_2$ is a circle with a radius of $r_2 \le R$. The probability of picking within this region is $$P(0 \le r \le r_2) = \frac{\pi r_2^2}{\pi R^2} = \frac{r_2^2}{R^2}.$$ Setting these probabilities equal to each other, we get $$u_2=\frac{r_2^2}{R^2}$$ $$r_2 = R\sqrt{u_2}$$ So, the function we want is $r = f(u) = R\sqrt{u}$.
We can do the same thing for the angle. The area of a circle section with an angle $\theta$ is $$A(\theta) = \frac{\theta}{2\pi}\pi R^2 = \frac{1}{2}\theta R^2.$$ Using the same probability trick above $$P(0 \le u \le u_0) = P(0 \le \theta \le \theta_0)$$ $$u_0 = \frac{\frac{1}{2}\theta_0 R^2}{\pi R^2}$$ $$u_0 = \frac{\theta_0}{2\pi}$$ $$\theta_0 = 2\pi u_0$$ So, the function $g(u) = 2\pi u$ is our function for randomly picking the angle $\theta$.
The point is that the area of the circle of radius $r$ is $\pi r^2$, and you want the probability of distance $\le r$ from the centre to be proportional to that area.