To make this more specific, I show a broken procedure for generating random points in a circle and a correct (hopefully) procedure for generating random dates within an interval.
I'd like to be able to precisely explain why one of them is wrong and the other is not, given that they sound very similar. What is so special about polar coordinates, that is not true about the case with dates?
Point in Circle
When placing a random point within a circle, the following is incorrect approach.
Use polar coordinates. First, generate the distance from center of the circle as number in interval [0, r). Then, generate the angle as number in interval [0, 2*pi).
The problem with the method described is that half of such points would lie within distance r/2 from the center, but that is only 1/4 of the surface of the whole circle.
(Anyways, how can one come up with such argument or know for certain there isn't one? It is obvious when it is stated, but I cannot imagine coming up with it myself; I'd just accept the method as correct.)
Random Date
randomdate = startdate + new TimeInterval(
days: random(from: 0 to: (enddate - startdate).days)
hours: random(from: 0 to: 23)
minutes: random(from: 0 to: 59)
)
When proving uniform distribution of values, what exactly am I trying to prove (how come that in the circle example I have to think of area density, which is not necessary in the date example) and how do I go about it, in a general case?




In the nonuniform point-in-circle example, what you do is take a uniform distribution of points on the rectangle $[0, R) \times [0, 2 \pi)$, and map them into the disc using the map $$ f(r, \theta) = (r \cos \theta, r \sin \theta).$$ The Jacobian of this map measures how "dense" the image is at a point compared to the source: we have $$ |D_f(r, \theta)| = \left \lvert \begin{matrix} \frac{\partial f_1}{\partial r} & \frac{\partial f_1}{\partial \theta} \\ \frac{\partial f_2}{\partial r} & \frac{\partial f_2}{\partial \theta} \end{matrix} \right \rvert = \left \lvert \begin{matrix} \cos \theta & -r \sin \theta \\ \sin \theta & r \cos \theta \end{matrix} \right \rvert = r (\cos^2 \theta + \sin^2 \theta) = r $$ and so there is a "stretch factor" independent of the angle, but proportional to the distance from the centre. A way to think about this is that if there was a 1cm coating of paint on the original rectangle $[0, R) \times [0, 2 \pi)$, and then we applied $f$, the paint on the resulting disc would only be $1/r$ cm thick at the point $(r \cos \theta, r \sin \theta)$.
A way to fix this is to use a modified map, corrected for this. For example, if we take $$ g(r, \theta) = (\sqrt{r} \cos \theta, \sqrt{r} \sin \theta) $$ then we find $$ |D_g(r, \theta)| = \left \lvert \begin{matrix} \frac{\partial g_1}{\partial r} & \frac{\partial g_1}{\partial \theta} \\ \frac{\partial g_2}{\partial r} & \frac{\partial g_2}{\partial \theta} \end{matrix} \right \rvert = \left \lvert \begin{matrix} \frac{\cos \theta}{2 \sqrt{r}} & -\sqrt{r} \sin \theta \\ \frac{\sin \theta}{2 \sqrt{r}} & \sqrt{r} \cos \theta \end{matrix} \right \rvert = \frac{1}{2} (\cos^2 \theta + \sin^2 \theta) = \frac{1}{2} $$ And so we get an even distribution of paint (onto disc of radius $\sqrt{R}$, rather than $R$).