1. The Fundamental Flaw of the Rectangular Assumption
On a 2D map projection (such as the Mercator projection), Earth appears as a flat rectangle with latitude running from -90° to +90° and longitude running from -180° to +180°.
Many programming tutorials advise generating random geographic coordinates using naive flat math:
// ❌ Naive algorithm (Suffers from polar bias bug) const latitude = (Math.random() * 180) - 90; const longitude = (Math.random() * 360) - 180;
While this code produces numbers within valid ranges, it treats all latitude bands as having equal physical area. On a real sphere, the circumference of a latitude ring shrinks as a function of the cosine of the latitude:
At the equator (0°), the circumference is approximately 40,075 km. At 80° North (near Greenland), the circumference shrinks to only 6,950 km. Because the naive algorithm distributes points uniformly by degrees rather than by surface area, a point is 5.7 times more dense at 80°N than at the equator!
2. The Marsaglia Equal-Area Spherical Transform
To generate points uniformly distributed over the surface of a sphere, we must ensure that the probability density is proportional to the differential surface area element:
Because the surface area element is proportional to d(sin θ), we can choose sin(θ) uniformly in the interval [-1, 1]:
// ✅ Correct Archimedes-Lambert-Marsaglia equal-area algorithm:
function getRandomSpherePoint() {
const u = Math.random(); // Uniform in [0, 1)
const v = Math.random(); // Uniform in [0, 1)
// Invert the cumulative distribution function:
const sinLat = 2 * u - 1; // Uniform in [-1, 1]
const latRad = Math.asin(sinLat); // In radians
const lat = latRad * (180 / Math.PI); // In degrees [-90, +90]
const lng = -180 + v * 360; // In degrees [-180, +180]
return { lat, lng };
}This elegant transformation guarantees that every square kilometer on Earth has an identical probability of selection.
3. Land Rejection Sampling & PRNG Reproducibility
Roughly 71% of Earth’s surface is covered by ocean water. When a user requests a random point on land only, we utilize rejection sampling:
- Generate an equal-area spherical point.
- Test against continental polygon bounding boxes and natural landmass vectors.
- If the point falls on open ocean, discard and regenerate with a maximum threshold of 250 attempts.
For reproducibility, our engine utilizes a 53-bit cyrb53 string hash combined with a 32-bit mulberry32 pseudorandom number generator (PRNG), ensuring that test runs and shared seed URLs produce 100% deterministic results.