Screen Shot 2022-04-06 at 11.15.38 AM

What is the average distance between 2 points in a rectangle?

Say we have a rectangle, and choose any 2 random points within it.  We then could calculate the distance between the 2 points.  If we do this a large number of times, what would the average distance between the 2 points be?

Monte Carlo method

The Monte Carlo method is perfect for this – we can run the following code on Python:

Screen Shot 2022-04-06 at 11.33.05 AM

This code will find the average distance between 2 points in a 10 by 10 square.  It does this by generating 2 random coordinates, finding the distance between them and then repeating this process 999,999 times.  It then works out the average value.  If we do this it returns:

Screen Shot 2022-04-06 at 11.36.13 AM

This means that on average, the distance between 2 random points in a 10 by 10 square is about 5.21.

Generalising to rectangles

I can now see what happens when I fix one side of the rectangle and vary the other side.  The code below fixes one side of the rectangle at 1 unit, and then varies the other side in integer increments.  For each rectangle it then calculates the average distance.

Screen Shot 2022-04-06 at 11.38.05 AM

This then returns the first few values as:

Screen Shot 2022-04-06 at 11.40.04 AM

This shows that for a 1 by 1 square the average distance between two points is around 0.52 and for a 1 by 10 rectangle the average distance is around 3.36.

Plotting some Desmos graphs

Screen Shot 2022-04-05 at 8.32.49 PM

Because I have included the comma in the Python code I can now copy and paste this straight into Desmos.  The dotted green points show how the average distance of a 1 by x rectangle changes as x increases.  I then ran the same code to work out the average distance of a 10 by x rectangle (red), 20 by x rectangle (black), 30 by x rectangle (purple) and 100 by x rectangle (yellow).

We can see if we continue these points further that they all appear to approach the line y = 1/3 x (dotted green).  This is a little surprising – i.e when x gets large, then for any n by x rectangle (with n fixed), an increase in x by one will tend towards an increase in the average distance by 1/3.

Heavy duty maths!

There is actually an equation that fits these curves – and will give the average distance, a(X) between any 2 points in a rectangle with sides a and b (a≥b).  Here it is:

Screen Shot 2022-04-06 at 5.38.59 PM

I added this equation into Desmos, by changing the a to x, and then adding a slider for b.  So, when I set b=1 this generated the case when the side of a rectangle is fixed as 1 and the other side is increased:

Screen Shot 2022-04-06 at 8.00.28 PM

Plotting these equations on Desmos then gives the following:

Screen Shot 2022-04-06 at 5.46.51 PM

Pleasingly we can see that the points created by our Monte Carlo method fit pretty much exactly on the lines generated by these equations.  By looking at these lines at a larger scale we can see that they do all indeed appear to be approaching the line y = 1/3 x.

General equation for a square

We can now consider the special case when a=b (i.e a square).  This gives:

Screen Shot 2022-04-06 at 6.11.34 PM

Which we can simplify to give:

Screen Shot 2022-04-06 at 6.11.42 PM

We can see therefore that a square of side 1 (a=1) will have an average distance of 0.52 (2dp) and a square of side 10 (a=10) will have an average distance of 5.21  – which both agree with our earlier results.