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 is

.

That’s equal to some random number , so we need to solve for .

.

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)
```

### Like this:

Like Loading...

*Related*

Tags: Earth, geography, python, random location

This entry was posted on July 25, 2009 at 3:59 am and is filed under links. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

## Leave a Reply