Turf.jsRandom

turf.randomPosition

What is turf.randomPosition?

turf.randomPosition returns a bare [longitude, latitude] array — not a Feature — picked uniformly from inside a bounding box. It is the primitive that the other random helpers build on.

JavaScript
turf.randomPosition(bbox?) → Position

Parameters:

  • bbox[minX, minY, maxX, maxY] (default [-180, -90, 180, 90])

When would you use turf.randomPosition?

Use turf.randomPosition when you need a single raw coordinate pair — for picking a random seed location, jittering existing coordinates, or building custom random generators on top. If you want a Feature, wrap the result with turf.point; if you want many at once, call turf.randomPoint.

JavaScript
1const [lng, lat] = turf.randomPosition([-122.6, 37.6, -122.3, 37.9]);
2const pin = turf.point([lng, lat], { spawnedAt: Date.now() });

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/random and import with import { randomPosition } from '@turf/random'. It is also on the umbrella @turf/turf package.

Does it return a Point Feature?

No. It returns a bare coordinate array (Position). Wrap it in turf.point if you need a Feature for downstream Turf calls.

Is the distribution uniform on the sphere?

No, it is uniform in [lng, lat] rectangle space. At global scales the poles are heavily over-represented. For equal-area sphere sampling you must apply your own latitude correction.

When should I prefer turf.randomPoint?

If you need a Feature or many points at once, turf.randomPoint(count, { bbox }) is more convenient and returns a FeatureCollection. Use randomPosition only when you need raw coordinates or a single value.