turf.randomLineString
What is turf.randomLineString?
turf.randomLineString creates a FeatureCollection<LineString> of randomly generated, loosely walked lines. Each line is a small chain of vertices constrained by length and rotation limits — useful for demos, load tests, and map styling previews.
turf.randomLineString(count?, options?) → FeatureCollection<LineString>Options include:
bbox—[minX, minY, maxX, maxY](default[-180, -90, 180, 90])num_vertices— vertices per line (default 10)max_length— maximum degrees between consecutive vertices (default 0.0001)max_rotation— maximum radians of turn per segment (defaultMath.PI / 8)
When would you use turf.randomLineString?
Reach for turf.randomLineString when you need synthetic line data for a prototype — filling a MapLibre vector source with sample routes, stress-testing a rendering pipeline with a thousand lines, or generating fixtures for unit tests that iterate over LineStrings.
It is not a geodesic simulator — the generated lines are short and meandering by default, so they look like fine threads rather than real routes. Pair with turf.buffer or turf.length to inspect the output before using it for visual testing.
1const lines = turf.randomLineString(50, {
2 bbox: [-122.6, 37.6, -122.3, 37.9],
3 num_vertices: 20,
4 max_length: 0.01
5});FAQs
How do I install Turf.js to use this function?
Install the scoped package npm install @turf/random and import import { randomLineString } from '@turf/random'. The umbrella bundle exposes it as turf.randomLineString.
Can I seed the random generator for reproducible output?
No. @turf/random uses Math.random() internally and does not accept a seed. For reproducible fixtures, generate once, serialise to JSON, and commit the file.
Why do my lines look like tight squiggles?
The defaults (max_length: 0.0001, max_rotation: Math.PI/8) produce very short, sharply turning segments. Raise max_length to a few hundredths of a degree for lines that are visible at a city zoom.
When should I use turf.randomLineString instead of building lines by hand?
Use it for synthetic load and layout tests where geometric accuracy does not matter. For realistic routing demos, call a routing API or replay real GPX tracks — random lines will not follow roads or respect obstacles.