# Distance between points in unit square

Two points are chosen randomly in a unit square.

What is the probability that the distance between these 2 points is less than X (where $$X\in(0,\sqrt2]$$)?

I've got this problem from my mind, just for interest, but have some problems of solving it.

• Try drawing some pictures and see if they lead you to an integral or two. – Integrand Jul 1 at 20:49
• See paragraphs 3 and (mainly) 4 in this document – Jean Marie Jul 1 at 21:18
• – Henry Jul 1 at 22:17

You may find a simulation is quick and close enough.

You can consider a similar question on a line: if $$X_1$$ and $$X_2$$ are uniformly distributed then $$D_X= \sqrt{(X_1-X_2)^2} = |X_1-X_2|$$ has a triangular distribution with density $$2(1-d)$$ and cumulative distribution function $$\mathbb P(D_x \le d) = 2d-d^2$$ when $$0 \le d \le 1$$. You would then say $$D_X^2$$ had cumulative distribution function $$\mathbb P( D_X^2 \le s) = 2\sqrt{s}-s$$ and density $$\frac1{\sqrt{s}}-1$$ when $$0 \le s \le 1$$.

$$D_Y^2= {(Y_1-Y_2)^2} = |Y_1-Y_2|^2$$ has the same cumulative distribution function and density as $$D_X^2$$ and they are independent. So you use a convolution to find the distribution of $$D^2=D_X^2+D_Y^2$$ with density $$f_{D^2}(z) =\int\limits_{-\infty}^\infty f_{D_X^2}(s)f_{D_Y^2}(z-s)\, ds$$. Your question is in fact asking about the distribution of $$D=\sqrt{D^2}=\sqrt{D_X^2 +D_Y^2 }$$.

When $$0 \le z =d^2 \le 1$$, i.e. $$0 \le d \le 1$$, the convolution gives $$f_{D^2}(z) =\int\limits_{0}^z\left(\frac1{\sqrt{s}}-1\right)\left(\frac1{\sqrt{z-s}}-1\right) \, ds= z-4\sqrt{z}+\pi$$ and by integration $$\mathbb P(D^2 \le z)=\frac12 z^2 -\frac83z^{3/2} +\pi z$$ and by substitution $$\mathbb P(D \le d)=\frac12 d^4 -\frac83d^{3} +\pi d^2$$ noting that $$\mathbb P(D^2 \le 1)=\mathbb P(D \le 1)=\pi-\frac{13}{6}$$.

Meanwhile when $$1 \le z =d^2 \le 2$$, i.e. $$1 \le d \le \sqrt{2}$$, it gives $$f_{D^2}(z) =\int\limits_{z-1}^1\left(\tfrac1{\sqrt{s}}-1\right)\left(\tfrac1{\sqrt{z-s}}-1\right) \, ds=-z+4\sqrt{z-1}-4\tan^{-1}\left(\sqrt{z-1}\right)+\pi-2$$ and by integration and using $$\mathbb P(D^2 \le 1)=\pi-\frac{13}{6}$$ $$\mathbb P(D^2 \le z)=-\tfrac12 z^2 + \tfrac83 z\sqrt{z-1}+(\pi-2) z -4 z \tan^{-1}\left(\sqrt{z-1}\right) + \tfrac43 \sqrt{z-1} +\tfrac13$$ and by substitution $$\mathbb P(D \le d)=\\-\tfrac12 d^4 + \tfrac83 d^2\sqrt{d^2-1}+(\pi-2) d^2 -4 d^2 \tan^{-1}\left(\sqrt{d^2-1}\right) + \tfrac43 \sqrt{d^2-1} +\tfrac13$$ noting as expected that $$\mathbb P(D^2 \le 2)=\mathbb P(D \le \sqrt{2})=1$$.

So that exercise gives the cumulative distribution function for $$D$$ as requested in the question. While we are looking at this we may as well also find the density for $$D$$ by taking the derivative, which is

• $$2d^3-8d^2+2\pi d$$ when $$0 \le d \le 1$$
• $$-2d^3+8d\sqrt{d^2-1}-8 d \tan^{-1}\left(\sqrt{d^2-1}\right)+(2\pi-4) d$$ when $$1 \le d \le \sqrt{2}$$
• $$0$$ when $$d \le 0$$ or $$\sqrt{2} \le d$$

and looks like this, very like one of the density curves I drew in answer to another question using numerical techniques

• The convolution for $z \geq 1$ seems to be incorrect. A copy/paste error maybe? I get $$-z-2 + 4 \sqrt{z-1} - 4 \tan^{-1}(\sqrt{z-1}) + \pi.$$ – WimC Jul 2 at 5:38
• @WimC - Yes indeed a copy and paste error. I used the correct expression for the integration. Many thanks - now edited – Henry Jul 2 at 8:34
• @Henry :very nice. (+1) – tommik Jul 2 at 8:41