If you ask most web developers to pick a random coordinate on Earth, they usually write this:
// β Problem: Creates severe polar distortion and clusters around the poles
const lat = Math.random() * 180 - 90;
const lng = Math.random() * 360 - 180;
While intuitive, this is mathematically broken. Lines of longitude converge at the poles while lines of latitude remain parallel. A uniform degree grid over-samples the Arctic and Antarctic circles while heavily under-representing the equatorial belt.
When building Random Location Generator, our goal was an ultra-fast, deterministic interactive geographic engine capable of:
- Archimedean spherical equal-area distribution.
- Sub-millisecond Land vs. Ocean classification without heavy GeoJSON dependencies.
- UNCLOS 1982 maritime boundary calculations (from 12 nm territorial waters to 200 nm EEZs).
- 100/100 Core Web Vitals using Astro's Islands Architecture and Tailwind CSS v4.
1. Eliminating Polar Bias: Archimedes' Hat-Box Theorem
To sample uniformly over the surface of a 3D sphere, you project points onto an enclosing cylinder where surface areas correspond directly to vertical height $z \in [-1, 1]$:
$$\text{Latitude} = \arcsin(2u - 1) \times \frac{180}{\pi} \quad \text{where } u \sim \mathcal{U}(0, 1)$$
In TypeScript:
export function generateSphericalPoint(): { lat: number; lng: number } {
const u = Math.random();
const v = Math.random();
// Equal-area latitude distribution
const lat = Math.asin(2 * u - 1) * (180 / Math.PI);
// Uniform longitude distribution
const lng = v * 360 - 180;
return {
lat: Number(lat.toFixed(5)),
lng: Number(lng.toFixed(5))
};
}
This ensures every square kilometer on Earth has the exact same probability of being chosen.
2. The Diagonal Coastline Bounding Box Problem
Earth is ~71% water. If a user selects "Land Spot", a rectangular bounding box fails along diagonal coastlines (e.g., Chile or Norway). A standard box enclosing the territory inadvertently captures massive triangular wedges of open ocean.
Actual Coastline (Diagonal) Rectangular Bounding Box
Ocean / Land [------ Land Box ------]
/ | Ocean Error | |
/ Land | Triangle | Land |
/ | (Bug Zone) | |
Ocean / [----------------------]
The Fix: Jordan Curve Ray-Casting
Rather than pulling in 500 KB GIS libraries (like Turf.js or GDAL in WASM) that compromise bundle size and INP responsiveness, we implemented a lean two-tier system:
- Tier 1: Fast Axis-Aligned Bounding Box (AABB) envelope check ($<0.01\text{ ms}$).
- Tier 2: A Jordan Curve ray-caster verifying parity across vector boundary segments:
export function isPointInPolygon(point: [number, number], polygon: [number, number][]): boolean {
const [x, y] = point;
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i][0], yi = polygon[i][1];
const xj = polygon[j][0], yj = polygon[j][1];
const intersect = ((yi > y) !== (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
3. High Performance with Astro & Tailwind CSS v4
- Zero Baseline JavaScript: Layouts, documentation, and SEO structured schemas render statically with 0 KB of client runtime.
-
Selective Hydration: The Leaflet map engine and synthesizer hydrate exclusively on
client:idle. - CSS Hardware-Accelerated Cartography: Dark and high-contrast tile themes use GPU filters directly over OpenStreetMap raster tiles, requiring zero external map API keys.
Test the live application at Random Location Generator or explore the regional tool at Random Country Generator.

Top comments (0)