The Drift Log · Model independence and repeatable builds
Seeding Random Generators for Parallel Test Workers
15 November 2026 · 3 min read · 701 words · established

Seeding per‑worker RNGs makes parallel test runs reproducible byte‑for‑byte, eliminating cache misses in CI.
Unseeded RNGs break reproducibility when tests run in parallel
A test suite that spawns workers for speed often creates a new Math.random() in each process. The generator is seeded from the current time or OS entropy. When the scheduler changes, the order in which workers start changes, and each run produces a different sequence of numbers. The test output diverges, the generated artefacts shift by a few bytes, and the build can no longer be reproduced byte‑for‑byte.
The failure is not “flaky tests” in the usual sense. The logic is correct; the only variable is the random seed. Because the seed is implicit, the build artefacts become a moving target. In a CI pipeline that caches build artefacts, a later run may not hit the cache, inflating build time and cloud cost. The problem is amplified when the test suite is part of a larger reproducible‑build workflow that expects every step to be repeatable.
Per‑worker seeds give a stable contract
If each worker receives a deterministic seed, the random stream becomes a function of the worker index, not of the wall‑clock. The contract is simple: given the same list of worker IDs, the same sequence of random numbers will be produced. This makes the test run reproducible regardless of scheduling jitter.
A minimal implementation in Node looks like this:
import { createHash } from 'crypto';
function seedForWorker(id: number): number {
const hash = createHash('sha256')
.update(`worker-${id}`)
.digest('hex')
.slice(0, 8);
return parseInt(hash, 16);
}
function rng(seed: number) {
let state = seed;
return () => {
// xorshift32 – small, fast, repeatable
state ^= state << 13;
state ^= state >>> 17;
state ^= state << 5;
return (state >>> 0) / 0xffffffff;
};
}
const workerId = Number(process.env.JEST_WORKER_ID ?? 0);
const random = rng(seedForWorker(workerId));
Each worker derives its seed from a hash of its identifier. The hash function is deterministic, so the same identifier always yields the same seed. The rng closure returns a repeatable pseudo‑random number generator. All test code that previously called Math.random() can now use random(). The change is isolated to the seed‑initialisation block; the rest of the test suite stays untouched.
Hook the seed into the test runner
Most JavaScript test runners expose a hook that runs before any test file is evaluated. In Jest, the setupFilesAfterEnv array can point to a module that performs the seeding. In Mocha, a --require flag loads the same module. The pattern is identical for other languages: initialise the RNG in a global fixture that runs once per worker.
// jest.setup.ts
import './seededRng';
Add the path to setupFilesAfterEnv in jest.config.js. When the CI system launches the test job with --maxWorkers=4, each worker receives a distinct JEST_WORKER_ID. The seed generation runs before any test code, guaranteeing that all random calls are drawn from the same deterministic stream on every CI run.
Because the seed is derived from a stable identifier, the test artefacts—snapshots, generated files, hash‑based caches—remain byte‑identical across runs. The build becomes reproducible, and downstream steps that depend on those artefacts can safely cache their results.
How SHPBL solves the same problem out of the box
SHPBL ships a reusable library that supplies a seeded‑randomness utility designed for parallel test workers. The module follows the same per‑worker contract described above, but it is packaged with a certification harness that runs the library against a known test suite and records a CERTIFIED verdict. The harness guarantees that the generated artefacts are repeatable byte‑for‑byte, independent of any external model or runtime variance.
Developers can pull the library via the typed TypeScript client (@shpbl/sdk) or the plain HTTP API, and the same artefacts are available offline from the archive whose checksums are published in the root of trust. The method aligns with the pillar post on Model Independence and Repeatable Software Builds and demonstrates how owning the randomness logic eliminates hidden variance.
Try the free repository evaluation at /evaluation to see the library in action, or read the live run on the /harness page for a concrete example.
Action items for Monday
- Identify the test runner you use and locate its per‑worker identifier (e.g.,
JEST_WORKER_ID). - Add a small seed‑initialisation module that hashes the identifier into a 32‑bit integer.
- Replace
Math.random()calls with the seeded generator returned by that module. - Run the suite twice in parallel mode and verify that the artefacts are byte‑identical.
With these steps the test suite will produce reproducible builds, even when the CI scheduler reshuffles workers. The effort is a few lines of code; the payoff is a stable cache, predictable CI times, and confidence that your builds are truly repeatable.
This post supports the longer argument in Model Independence and Repeatable Software Builds.
Keep reading
Next in the log
- Model Independence and Repeatable Software Builds
Runtime model variance and non-reproducible builds compound to break production systems. Here is how to isolate probabilistic logic and pin artifacts.
- Capturing Build-Time Environment to Freeze Artifact Output
Scrub ambient host variables with an allowlist and record environment manifests to guarantee byte-for-byte repeatable build artifacts.
- Deterministic Ordering of JSON Keys for Stable Snapshots
Flaky CI snapshot diffs caused by nondeterministic JSON key order can be eliminated with SHPBL’s certified, repeatable canonicalisation routine.
The Strategic Master Library · written and reviewed under the house's own epistemic rules: nothing claimed that we cannot show.