Last Tuesday I was load-testing a booking API. The endpoint takes a seat number (1–250), an age (18–90) and an order amount (0–5000, two decimals). I needed 5,000 records that looked real, generated fast, and reproducible on rerun. My first generator produced data that looked fine for two minutes. Then the API rejected seat 251 and the whole test run died. So here are the three approaches I settled on, plus the boundary bugs that cost me an afternoon.
Method 1: Python 3.11
Python 3.11.1, standard library only, no dependencies:
import random
random.seed(42) # same fixture file on every run
def make_records(n: int) -> list[dict]:
return [
{
"id": i,
"seat": random.randint(1, 250), # both ends inclusive
"age": random.randint(18, 90),
"amount": round(random.uniform(0, 5000), 2),
}
for i in range(n)
]
if __name__ == "__main__":
import json
with open("fixtures.json", "w") as f:
json.dump(make_records(5000), f, indent=2)
The first few records look like this:
{"id": 0, "seat": 148, "age": 63, "amount": 2914.31},
{"id": 1, "seat": 7, "age": 22, "amount": 873.05}
random.randint(1, 250) includes both 1 and 250. That property is the entire reason I use it for test data. Its sibling random.randrange(1, 251) does the same job but the upper bound is exclusive, and that asymmetry is where most of my past off-by-one bugs started. The seed(42) call matters even more: when the load test fails on record 3,812, I rerun against the byte-identical fixture instead of chasing a moving target. If you need the details, the Python random module docs cover seeding and the integer helpers properly.
Method 2: Node 22
Same fixtures, Node 22.13.0:
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const records = Array.from({ length: 5000 }, (_, id) => ({
id,
seat: randInt(1, 250),
age: randInt(18, 90),
amount: Math.round(Math.random() * 5000 * 100) / 100,
}));
console.log(JSON.stringify(records.slice(0, 2), null, 2));
// [ { id: 0, seat: 91, age: 44, amount: 1203.77 },
// { id: 1, seat: 250, age: 78, amount: 4788.42 } ]
The max - min + 1 inside the floor call is the whole trick. Drop the +1 and seat 250 can never be drawn, no matter how many records you generate. The amount line multiplies by 100 first and divides after rounding, which keeps the two decimals honest; rounding a bare float gives you values like 2914.3099999999995, and my JSON comparison asserts hated that. One caveat: Math.random() cannot be seeded, so Node has no built-in way to reproduce a run. If I need reproducibility in Node, I reach for a seeded PRNG package rather than hand-rolling one — the MDN Math.random() page explains why it is not cryptographically secure either.
Method 3: a browser tool instead of a script
Half the time I don't need 5,000 records. I need twenty seat numbers to eyeball an API response, or thirty unique raffle picks for a demo. Opening an editor for that is overkill. I've started using this browser-based random number generator for those one-offs: set the range to 1–250, set the count, tick "no duplicates", tick "sort ascending", done. It runs entirely client-side, so it works offline and nothing gets uploaded anywhere. When I want a quick duplicate-free batch — say 40 unique numbers from 1–500 for a test roster — a free online random number tool is genuinely faster than writing the dedupe logic, because it also handles the sorting and the crypto-quality randomness in the same click.
Three bugs I hit before it worked
1. Math.random() * max never returns the maximum
My very first Node version did Math.round(Math.random() * 250) for seats. I "fixed" it to Math.random() * 250 without rounding, then checked whether seat 250 ever appeared:
let hits = 0;
for (let i = 0; i < 1_000_000; i++) {
if (Math.random() * 250 === 250) hits++;
}
console.log(hits);
// 0
One million draws, zero hits on the boundary. Math.random() returns a value in [0, 1), so multiplying can approach 250 but never reach it. The floor-based randInt above is the fix.
2. Math.round makes edge values half as likely
Even the rounded version had a subtler problem. Distribution over a million draws:
const counts = new Array(251).fill(0);
for (let i = 0; i < 1_000_000; i++) counts[Math.round(Math.random() * 250)]++;
console.log(counts[0], counts[1], counts[125], counts[250]);
// 1976 4057 4006 1994
Seat 0 and seat 250 each appeared about half as often as the interior values, because Math.round only reaches them across half-width slices. Nothing crashed; a seat histogram just looked slightly wrong and I spent an hour blaming the API. The floor formula gives every value the same slice width.
3. Same seed in parallel workers produced identical fixtures
For a parallel load run I seeded four Python workers with random.seed(42) each, to keep things "reproducible". Reproducible they were — byte-identical. Worker 2 and worker 3 wrote exactly the same 5,000 seats, and when both fixture files were loaded into SQLite, the second import died:
sqlite3.IntegrityError: UNIQUE constraint failed: bookings.id
The fix was one line: random.seed(base_seed + worker_id), with base_seed logged at startup. Every worker stays reproducible, and no two share a dataset.
Summary
| Task | Python 3.11 | Node 22 |
|---|---|---|
| Integer in [min, max], both ends included | random.randint(min, max) |
Math.floor(Math.random() * (max - min + 1)) + min |
| Float in [min, max] | random.uniform(min, max) |
Math.random() * (max - min) + min |
| Reproducible run |
random.seed(n) per worker, distinct seeds |
Not seedable in stdlib; use a seeded PRNG package |
| Quick one-off batch with dedupe | — | utilvy.com random number generator |
Rule of thumb now: scripts for bulk fixtures, the browser tool for spot checks, and seeds written down somewhere the team can find them.
More free browser-based utilities at utilvy.com.
Top comments (0)