## Random Spots on Earth

Don’t know where to take your next vacation?

Here’s some python code to generate a bunch of random locations on Earth. Copy and paste into python, run, then copy and paste a location into Wolfram Alpha or Google Maps. Google Maps has pictures, but is slower. Wolfram Alpha will tell you the country, and then you can click on a link to Google Maps (the link is just called “satellite image”) if it’s someplace interesting. Or, try guessing where the location is before you plug it in.

WordPress doesn’t allow javascript, so I can’t easily generate the locations for you or embed the Wolfram Alpha search bar. Too bad.

About generating random locations:
The longitude is easy, since you just pick a random number between -180 and 180 degrees.

Latitude is not so straightforward. If you draw a thin band (one mile wide, say) around the line of latitude at 60 degrees North, that band is only half as large as one around the equator, since it’s a shorter way around the globe up at 60 degrees. The size of the band at a certain latitude, relative to one at the equator, is the cosine of the latitude.

To get a random location on Earth (all locations being equally likely) we choose a random number from zero to one, and say that this is the percentage of land that lies south of our chosen location. The percentage of land south of a line of latitude $\theta$ is $\frac{1}{2}\int_{-90}^\theta \cos\theta' d\theta' = \frac{1}{2} (1 + \sin\theta)$.

That’s equal to some random number $r$, so we need to solve for $\theta$. $\theta = \arcsin(2r-1)$.

The code accomplishes the same thing in a slightly more convoluted way, because I originally solved it thinking of the north pole as zero radians (as we do in math) rather than 90 degrees (as we do in geography). Okay, here’s the code


import random
import math

for i in range(1,100):

long = random.random()*360 - 180
lat = math.acos(1-2*random.random())*180/3.14159-90

if(long>0):
ew = "E"
else:
long = -long
ew="W"
if(lat>0):
ns = "N"
else:
ns = "S"
lat = -lat

print "%.2f %s , %.2f %s" % (long,ew,lat,ns)