The Distance Between Two Randomly Selected Points in the Unit Square

I ran across a math problem that is simultaneously surprisingly easy and surprisingly difficult: “What is the average distance between two randomly selected points in the unit square?” One Sunday afternoon, instead of walking my dogs in the rain, I decided to explore.

Stanislaw Ulam in front of the early MANIAC computer.

The unit square is the square where the x coordinate is between 0 and 1, and the y coordinate is between 0 and 1. So (0.5, 0.3) is in the unit square but (0.7, 1.2) is not in the unit square. Calculating the answer exactly is quite difficult and involves a quadruple integration. But writing a computer simulation to estimate the answer is trivial.

The exact answer can be found in many references on the Internet. It is

(2 + sqrt(2) + 5*ln(sqrt(2) + 1)) / 15 = 0.52140543

Computing the answer using Calculus, from one of several explanations I found on the Internet.

I wrote a simulation where I iterated 10,000,000 times. In each iteration, I generated two random points in the unit square and computed the distance between them. I saved each distance and computed the average of the 10,000,000 trials and got 0.52150332 which is accurate to 0.0001. This technique is sometimes called the Monte Carlo method. Stanislaw Ulam invented the computer version of the Monte Carlo method while he was working on nuclear weapons projects at the Los Alamos National Laboratory, in the 1940s.

A simple estimate using a Python program.

There are many examples of problems like this where a math-based solution is very difficult but a computer simulation is simple.

There were three science fiction movies released in 1999 that all featured plots with virtual reality simulations. Left: In “The Thirteenth Floor” a billionaire computer guy invents a virtual reality simulation of 1930s Los Angeles, where the simulated humans don’t know they’re programs. The inventor is murdered and his partner is accused of the murder. The complicated plot with worlds within worlds was a bit too confusing for me. Center: In “eXistenZ” virtual game companies create extremely realistic simulations. Ted (Jude) Law and Allegra (Jennifer Jason Leigh) enter a game and all kinds of bizarre things happen in the simulation. Interesting movie but I thought the ending was ambiguous, and I don’t like ambiguous endings. Right: Everyone I know has seen “The Matrix”, usually several times. A great virtual reality simulation movie.

This entry was posted in Miscellaneous. Bookmark the permalink.

1 Response to The Distance Between Two Randomly Selected Points in the Unit Square

  1. saurabh dasgupta says:

    Nice one James. When I first encountered the problem , I did not believe there could be any sensible pattern in this. But, this is another example of the unreasonable effectiveness of maths…

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s