Skip to content

Instantly share code, notes, and snippets.

@justin
Created May 9, 2013 04:16
Show Gist options
  • Save justin/5545529 to your computer and use it in GitHub Desktop.
Save justin/5545529 to your computer and use it in GitHub Desktop.
Generate a shit load of random geocoordinates around a given distance.
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
#define ARC4RANDOM_MAX 0x100000000
double startingLatitude = DEGREES_TO_RADIANS(39.753638);
double startingLongitude = DEGREES_TO_RADIANS(-105.007375);
double earthRadius = 3960.056052;
double maxdist = 50.0f;
maxdist = maxdist / earthRadius;
for (int i = 0 ; i<100 ; i++)
{
double random1 = ((double)arc4random() / ARC4RANDOM_MAX);
double random2 = ((double)arc4random() / ARC4RANDOM_MAX);
double randomDistance = acos(random1 *(cos(maxdist) - 1) + 1);
double brg = 2 * M_PI * random2;
double lat = asin(sin(startingLatitude) * cos(randomDistance) + cos(startingLatitude) * sin(randomDistance) * cos(brg));
double lon = startingLongitude + atan2(sin(brg) * sin(randomDistance) * cos(startingLatitude), cos(randomDistance) - sin(startingLatitude) * sin(lat));
if (lon < (M_PI * - 1))
{
lon = lon + 2 * M_PI;
}
if (lon > M_PI)
{
lon = lon - 2 * M_PI;
}
CLLocationCoordinate2D coord;
coord.latitude = RADIANS_TO_DEGREES(lat);
coord.longitude = RADIANS_TO_DEGREES(lon);
}
@lvandyk
Copy link

lvandyk commented Jul 22, 2015

Have used this multiple times! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment