Foundations to Frontier
Tests
C. Programming, Tools and HPC › C2 Software Engineering for Research

Reproducibility: Seeds, Determinism and Environment Pinning

A computational result is only as trustworthy as your ability to get it again. This topic covers the sources of irreproducibility (unseeded or shared random state, floating-point non-associativity under parallel reduction order, nondeterministic GPU kernels, drifting dependencies, hidden data versions) and the remedies: local Generators and SeedSequence.spawn for parallel streams, deterministic algorithms and tolerance-based comparison, pinned and locked environments with fingerprints, recorded configurations and run IDs, versioned data. In the exercises you build a seeded experiment with parallel streams, an environment fingerprint and deterministic run IDs, then measure how much summation order alone changes a result, and how pairwise and exact (fsum) summation fix it.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Getting the same answer twice

You run a simulation and get 133.8 m of landing error. A colleague runs “the same thing” and gets 134.1. Who’s right? Maybe both: the random numbers differed, or a library updated, or the computer added numbers in a different order. Reproducibility means recording and controlling everything that affects the result (the random seed, the exact software versions, the input data and the settings) so anyone can get the same answer again, or explain why not.

Same numbers, different order, different sum
naivepairwisecompensateddeviation from the accurate sum (ulps); ±22 shown
spread over 60 orderings: naive 38 ulp · pairwise 4 ulp · compensated 0 ulp
Floating-point addition rounds after every step, so (a + b) + c ≠ a + (b + c) in general. Multi-threaded and GPU reductions add in whatever order the hardware finishes, so results can change from run to run even with fixed seeds. The fixes: accept and test within tolerances; use pairwise or compensated sums (math.fsum, which is exactly rounded); or force deterministic algorithms (fixed reduction order; torch.use_deterministic_algorithms) when bit-exact reproducibility is required.
Why engineers care

Certification evidence, Monte Carlo dispersion analyses for landing ellipses, and ML results in papers must be re-creatable months later by someone else. Irreproducible pipelines waste weeks chasing “bugs” that are really changed seeds, library versions or summation order, and they undermine trust in results.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A random seed…
  2. 2. Pinning dependencies means…
  3. 3. Can the order of additions change a floating-point sum?

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Randomness done right

import numpy as np
rng = np.random.default_rng(42)          # a local Generator: pass it around explicitly
x = rng.normal(0, 50, size=1000)

ss = np.random.SeedSequence(42)          # parallel workers: independent child streams
rngs = [np.random.default_rng(s) for s in ss.spawn(8)]

Avoid the legacy global state (np.random.seed, random.seed): any library call that draws from it silently shifts your stream. Don’t use seed + worker_id (streams can overlap or correlate). Seed every source: Python’s random, NumPy, PyTorch (torch.manual_seed, plus CUDA) and data-loader workers.

What to record for every run

Code version (git commit, clean working tree), environment (lock file, plus a fingerprint), full configuration (C2.5), random seeds, input-data version or hash, hardware/driver for GPU work, and the outputs with a run ID.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Best way to give 8 parallel workers independent random streams:
  2. 2. Why avoid np.random.seed?
  3. 3. Which is NOT needed to reproduce a run?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Determinism vs reproducibility

  • Bitwise determinism: identical bits every run on the same platform. It needs fixed seeds, single-threaded or deterministic reductions, and deterministic GPU kernels (torch.use_deterministic_algorithms(True), CUBLAS_WORKSPACE_CONFIG), often at some cost in speed.
  • Statistical reproducibility: results agree within stated tolerances or confidence intervals across platforms. This is usually the realistic goal across machines, BLAS libraries and GPUs. Compare with tolerances, report variability over seeds.

Floating-point order effects

Each addition rounds to about 16 significant digits, so the order matters, especially with large dynamic range. Parallel reductions (OpenMP, MPI, GPU atomics) use whatever order the hardware produces. Remedies: pairwise summation (NumPy’s default, error growing like O(log⁡n)O(\log n)), compensated summation (Kahan/Neumaier; Python’s built-in sum of floats is compensated since 3.12), exactly rounded math.fsum, or a fixed reduction tree.

🐍 Exercise 1: a reproducible experiment
Write a seeded Monte Carlo experiment, reproducible parallel streams via SeedSequence.spawn, a demonstration that the legacy global RNG is fragile while local Generators are isolated, an environment fingerprint (Python, platform, package versions) and a deterministic run ID from config + fingerprint.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Results agree to 1e-12 on one machine but differ at 1e-10 on another. Most likely cause:
  2. 2. math.fsum is…
  3. 3. For cross-platform checks, compare results…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Error bounds for summation

Summing n numbers naively has worst-case error up to about (n−1) u∑∣xi∣(n - 1)\,u\sum|x_i| (u = 2⁻⁵³), and the actual error depends on the order. Pairwise summation reduces this to O(ulog⁡n)∑∣xi∣O(u\log n)\sum|x_i|, and compensated summation to O(u)∑∣xi∣+O(nu2)O(u)\sum|x_i| + O(nu^2). When ∑∣xi∣\sum|x_i| greatly exceeds ∣∑xi∣|\sum x_i| (cancellation, large dynamic range), these bounds are large relative to the result, which is exactly when order effects show.

Why SeedSequence beats seed + i

SeedSequence hashes the entropy (and the spawn key) through a mixing function to initialize each child’s state, so nearby integers don’t produce nearby or correlated states. PCG64 streams from different well-mixed states are practically independent.

The reproducibility stack

Data version (DVC or a hash) → code (git commit) → environment (lock file plus container) → configuration (C2.5) → seeds → outputs with a run ID derived from all of the above. If any input changes, the ID changes, which makes caching safe.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Order effects are largest when…
  2. 2. Pairwise summation's error grows like…
  3. 3. A run ID derived from config + environment + data hash lets you…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Reproducibility, measured

Exercise 1. experiment(42) returns the same (mean, 99th percentile) on every call, (50.36, 133.82) m with NumPy 2.x. Spawned worker streams are reproducible and distinct. With the global RNG, a single extra draw by “some library” shifts every subsequent number, while independent Generators don’t interfere. The environment fingerprint (Python version, platform, NumPy version) feeds a run ID that ignores dictionary key order but changes with any setting.

Exercise 2: summation order (20 000 values: 1% of size ~10⁶, the rest ~10⁻³; spread over 20 random orderings, in units in the last place of the exact sum):

method spread
naive left-to-right loop 144 ulp
NumPy pairwise sum 13 ulp
math.fsum 0
naive sum split into 1–64 “parallel” chunks 73 ulp

So merely changing the thread count changes the 8th significant digit of this sum. fsum is order-independent because it is exactly rounded. (Browser numbers: NumPy’s pairwise blocking differs between builds, so the pairwise spread differs slightly by platform, 11 ulp on desktop CPython.)

🐍 Exercise 2 (research): how much does order matter?
Build data with a large dynamic range, compute its exactly rounded sum with math.fsum, then measure the spread (in ulps) of naive sums over random orderings, NumPy's pairwise sum, fsum, and chunked 'parallel' sums with different numbers of chunks.

Reproducibility in AI, robotics and aerospace

  • Monte Carlo dispersion analyses for entry, descent and landing must be reproducible for review.
  • ML experiments: seeds for data order, initialization and augmentation; report mean ± spread over seeds.
  • Simulation regression tests (C2.2) need tolerances that reflect order effects, not exact equality.
  • Containers plus lock files plus DVC for long-lived mission analyses.

References

  1. N. J. Higham (1993). “The accuracy of floating point summation.” SIAM Journal on Scientific Computing, 14(4).
  2. D. Goldberg (1991). “What every computer scientist should know about floating-point arithmetic.” ACM Computing Surveys, 23(1).
  3. NumPy documentation: Random sampling: SeedSequence and parallel generation.
  4. PyTorch documentation: Reproducibility (torch.use_deterministic_algorithms).
  5. G. K. Sandve, A. Nekrutenko, J. Taylor, E. Hovig (2013). “Ten simple rules for reproducible computational research.” PLOS Computational Biology, 9(10).
  6. J. R. Shewchuk (1997). “Adaptive precision floating-point arithmetic and fast robust geometric predicates.” Discrete & Computational Geometry, 18 (the basis of math.fsum).