turf.randomPoint
What is turf.randomPoint?
turf.randomPoint creates a FeatureCollection<Point> of uniformly random points within a bounding box. It is the fastest way to produce synthetic point datasets for prototypes and performance tests.
turf.randomPoint(count?, options?) → FeatureCollection<Point>Options include:
bbox—[minX, minY, maxX, maxY](default[-180, -90, 180, 90])
When would you use turf.randomPoint?
Use turf.randomPoint whenever you need synthetic point data — loading a demo map with 10,000 sample markers, benchmarking a clustering algorithm, generating fixtures for turf.pointsWithinPolygon, or populating a heatmap layer to preview styling.
If you need points distributed inside a non-rectangular area, generate in the bounding box and filter with turf.booleanPointInPolygon — repeat until you reach the desired count.
1const sample = turf.randomPoint(1000, { bbox: [-122.6, 37.6, -122.3, 37.9] });
2map.getSource('demo').setData(sample);FAQs
How do I install Turf.js to use this function?
Install npm install @turf/random and import via import { randomPoint } from '@turf/random', or use the umbrella @turf/turf package.
Is the distribution statistically uniform across the earth?
The points are uniform in [lng, lat] space, not on the sphere. Global bounding boxes over-sample the poles. For equal-area sampling, generate in an equal-area projection with turf.toMercator-style custom math, or reject-sample by latitude.
Can I include elevation or extra properties?
Not directly. randomPoint produces plain points with empty properties. Map over the output to attach properties or a third coordinate value before using the features downstream.
How does it compare to turf.sample?
turf.randomPoint invents new points from scratch. turf.sample picks existing features from a FeatureCollection you already have. Use randomPoint for synthetic data, sample for drawing from a real dataset.