Dear Uncle Colin,

In one of Randall Munroe’s What If? articles he says that the maths of finding a random point on a sphere is a bit hairy. Can’t you just pick a random latitude and longitude?

-- Surely Places Have Equal Random Expectations

You would think so, wouldn’t you, SPHERE? This is the problem with having been brought up on the Mercator projection and the belief that the earth – although not flat – may be easily flattened.

But I digress.

The reason the ‘obvious’ scheme doesn’t work is, lines of latitude ((the east-west ones that go LATerally)) aren’t all the same length. The equator is significantly longer than the Tropic of Cancer, which is in turn longer than the Arctic Circle, which is in turn longer than the line of latitude at the North Pole, the length of which is zero. That means, if you want a uniformly random distribution on a sphere, you need to make the equator more likely to show up than any other line of latitude, and the other lines in proportion.

How long is each line of latitude? That’s easy enough with a bit of trigonometry. The Arctic Circle, for instance, is at 63.5º ((Why they don’t use radians, I don’t know)) north, so we can make a right-triangle using a line from the centre of the earth to a point on the circle, from there to the Earth’s axis, and back to the centre of the Earth. The radius of the Arctic Circle is $R_E \cos(63.5º)$ – and similarly for any line of latitude you pick. The length of any circle of latitude is proportional to the cosine of the latitude.

So how do we pick a random line of latitude? That’s simpler than Munroe makes out: all you need to do is generate a random number between -1 and 1, and work out its inverse cosine. (This will give you an angle between 0º and 180º, so you’ll need to subtract 90º from your answer.)

As for the lines of longitude? They’re all the same length, so you can just pick those uniformly at random.

-- Uncle Colin

* Hungry for more What Ifs? You can buy the book!

* Thanks to @michiexile for pointing out a mix-up between longitude and latitude.