# Calculation Methods - Random Points

This page describes the two calculation methods used in the Random Point Generator, a utility that allows you to generate one or more random points anywhere on the earth's surface. This is in essence like throwing one or more virtual darts at a world map, with the option of viewing the generated random points on a Google map.

Because the utility assumes that the earth is a sphere rather than a flat map, all locations on the surface of the earth have an equal probability of being chosen.

## A. Circular region

With this method you can restrict the generated random points to a circular region that you specify, or you can extend it to include the whole earth. You enter the latitude and longitude of a starting point and a maximum distance which is the radius of a circle centered on the starting point.

To help visualize how this method chooses a random point when the whole earth is selected, imagine that a tiny circle is drawn around the starting point, then larger and larger concentric circles are drawn until halfway across the earth a circle with the circumference of the earth is reached. Then the circles gradually become smaller until the point on the opposite side of the earth from the starting point is reached. The larger the diameter of a circle, the more likely that a random point on that circle will be chosen.

### Circular region calculation detail

1. Convert all latitudes and longitudes to radians.
2. rand1 and rand2 are unique random numbers generated in the range 0 to 1.0.
3. Given the initial values startlat, startlon and maxdist. (maxdist is in miles or kms).
4. For the mean radius of the earth use:
radiusEarth = 3960.056052 miles or radiusEarth = 6372.796924 km
5. Convert maximum distance to radians.
6. Compute a random distance from 0 to maxdist scaled so that points on larger circles have a greater probability of being chosen than points on smaller circles as described earlier.
dist = acos(rand1*(cos(maxdist) - 1) + 1)
7. Compute a random bearing from 0 to 2*PI radians (0 to 360 degrees), with all bearings having an equal probability of being chosen.
brg = 2*PI*rand2
8. Use the starting point, random distance and random bearing to calculate the coordinates of the final random point.
lat = asin(sin(startlat)*cos(dist) + cos(startlat)*sin(dist)*cos(brg))
lon = startlon + atan2(sin(brg)*sin(dist)*cos(startlat), cos(dist)-sin(startlat)*sin(lat))
9. If lon is less than -PI then:
lon = lon + 2*PI
If lon is greater than PI then:
lon = lon - 2*PI

## B. Rectangular region

This method allows you to restrict the generated random points to a rectangular area on the earth's surface that you define, or you can enlarge the rectangle to include the whole earth. You specify latitudes for the north and south sides of the rectangle, and longitudes for the west and east sides of the rectangle. Note: this is not a true rectangle because latitude and longitude lines are curved and longitude lines converge at the poles, however it is often a convenient way to define a region on the earth's surface and it does provide a way to generate true random points on a spherical earth.

The calculation is based on the fact that latitude lines vary in length, with the longest latitude being the equator and the other latitude lines diminishing in length according to a trigonometric function until the poles are reached, with both poles having a zero length latitude line. The random longitude is easily calculated, all longitude lines in a rectangular region have an equal probability of being chosen.

### Rectangular region calculation detail

1. Convert all latitudes and longitudes to radians.
2. rand1 and rand2 are unique random numbers generated in the range 0 to 1.0.
3. Given the initial latitudes northlimit and southlimit, and the longitudes westlimit and eastlimit.
4. Compute a random latitude such that points on longer latitude lines in the rectangle are more likely to be chosen than points on shorter latitude lines.
lat = asin(rand1*(sin(northlimit) - sin(southlimit)) + sin(southlimit))
5. Find the width of the rectangular region.
width = eastlimit - westlimit
6. If width is less than 0 then:
width = width + 2*PI
7. Compute the random longitude between westlimit and eastlimit with all longitudes having equal probability of being chosen.
lon = westlimit + width*rand2
8. If lon is less than -PI then:
lon = lon + 2*PI
If lon is greater than PI then:
lon = lon - 2*PI