Random Seeds and Why Two Training Runs Disagree
Find every source of randomness in a training run, control what you can, and measure seed variation before you believe a gain.
Key takeaways
- Initialization, data splits, shuffling, dropout, augmentation and sampling inside algorithms all consume random numbers.
- A fixed seed makes a run repeatable only if the code, data order, library versions and hardware behavior also stay the same.
- Some parallel and GPU operations can differ between runs even with identical seeds, unless you ask for deterministic versions.
- A single seed is one draw from a distribution of outcomes, so compare methods over several seeds, paired where possible.
- Picking the best-scoring seed and reporting it overstates how well a method works.
Two runs of the same training script disagree because training makes many random choices, and some hardware operations add small differences of their own. A random seed is the starting value of a pseudorandom number generator, a deterministic algorithm whose output looks random. Fix the seed and the generator repeats the same sequence.
Seeding helps you reproduce a run. It does not tell you whether the result is typical. For that you need to run several seeds and report the spread, especially when the improvement you are excited about is small.
Where the randomness comes from
It helps to list the places a typical project draws random numbers, because each one can move your score.
| Source | What changes between seeds |
|---|---|
| Train, validation and test split | Which examples the model learns from and is scored on |
| Weight initialization | The starting point of optimization in a neural network |
| Shuffling and mini-batches | The order of gradient updates |
| Dropout | Which units are switched off at each step |
| Data augmentation | The crops, flips or noise applied to each example |
| Bagging and feature sampling | Which rows and columns each tree sees |
| Hyperparameter search | Which settings get tried |
Some methods are far less sensitive than others. Ordinary least squares solved directly on a fixed data set has no random step. A neural network trained on a few thousand examples can vary a lot, because both the starting weights and the batch order steer it toward different solutions.
The split deserves special attention. If your validation set is small, moving a few hard examples into or out of it can shift the score more than any modeling change.
See the effect of the split alone
The script below keeps the model and the data fixed and changes only the split seed. It creates 600 examples with 20 features, so X has shape (600, 20) with one example per row.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=600, n_features=20,
n_informative=5, random_state=0)
scores = []
for seed in range(30):
X_tr, X_te, y_tr, y_te = train_test_split(
X, y, test_size=0.25, random_state=seed)
model = LogisticRegression(max_iter=1000).fit(X_tr, y_tr)
scores.append(model.score(X_te, y_te))
scores = np.array(scores)
print(scores.min(), scores.mean(), scores.max())
print(scores.std(ddof=1)) Run it and look at the distance between the lowest and highest accuracy. Each test set has only 150 examples, so one example is worth about 0.67 percentage points.
You can estimate this noise without running anything. For accuracy p measured on n independent test examples, the standard error is about √(p(1 − p)/n). With p = 0.85 and n = 1,000, that is √(0.1275/1000) ≈ 0.011, or about 1.1 percentage points.
What a seed does and does not guarantee
A generator holds internal state. Every call advances the state, so the values you get depend on both the seed and how many calls came before. Add one extra random call early in your script, and everything after it can change even though the seed did not.
A cleaner habit is to give each component its own generator:
import numpy as np
root = np.random.SeedSequence(2024)
split_ss, init_ss, batch_ss = root.spawn(3)
split_rng = np.random.default_rng(split_ss)
init_rng = np.random.default_rng(init_ss)
batch_rng = np.random.default_rng(batch_ss) Now changing how batches are drawn leaves the split and the initial weights untouched.
Many projects use several libraries, each with its own generator: Python's random module, NumPy, and a deep learning framework. Seeding one does not seed the others. Data loading workers may need their own seeding too. Python also randomizes string hashing between interpreter sessions by default, which can reorder iteration over sets of strings. The PYTHONHASHSEED environment variable controls that, and it must be set before Python starts.
Why identical seeds can still disagree
Floating-point addition is not associative. In double precision, (0.1 + 0.2) + 0.3 and 0.1 + (0.2 + 0.3) print different results. When a GPU or a multithreaded library adds millions of numbers in whatever order the threads finish, the last digits can vary.
Those tiny differences feed into the next weight update, then the next. In a long training run they can grow into visibly different final models, with scores that differ about as much as two different seeds would.
Frameworks usually offer settings that force deterministic algorithms. These can slow training, and a few operations may have no deterministic version, in which case you may get an error or a warning. The exact flags change between releases, so check the current documentation for your framework and hardware.
Results can also shift when you change the library version, the number of threads, the batch size or the device. Treat bit-for-bit reproduction as a property of one pinned environment. Pinning environments along with seeds is how you keep it.
What to record for every run
A seed alone cannot rebuild a result. Store these next to each score, ideally written by the script itself rather than by hand:
- every seed, including the ones for splitting and data loading;
- the code version, such as a commit identifier, and any uncommitted changes;
- an identifier or hash of the data snapshot, plus the row order if it matters;
- library versions, hardware type, thread count and any determinism settings;
- the full configuration, including defaults you did not set yourself.
Saving the actual row identifiers of each split is cheap and protects you if a library later changes how it shuffles.
Compare two methods over several seeds
Suppose you compare a baseline A with a change B, using five seeds. Each seed fixes the split for both methods. These are example numbers.
| Seed | A | B | B − A |
|---|---|---|---|
| 1 | 0.842 | 0.849 | 0.007 |
| 2 | 0.851 | 0.855 | 0.004 |
| 3 | 0.838 | 0.846 | 0.008 |
| 4 | 0.847 | 0.852 | 0.005 |
| 5 | 0.849 | 0.855 | 0.006 |
The means are 0.8454 for A and 0.8514 for B. The sample standard deviations are about 0.0053 and 0.0039, which is close to the size of the gap. Looking only at those two summaries, you might call it a tie.
The paired differences tell a sharper story. Their mean is 0.006 and their standard deviation is about 0.0016, because both methods rise and fall together with the split. The standard error is 0.0016/√5 ≈ 0.0007. With four degrees of freedom, a 95% t interval is 0.006 ± 2.776 × 0.0007, roughly 0.004 to 0.008.
Read that interval with care. It assumes the differences are roughly normal and independent, and random splits of one data set overlap, so it tends to be optimistic. It also says nothing about new data from a different source. Confidence intervals for test metrics covers the sampling side in more detail.
Report seeds honestly
A few habits keep seed variation from fooling you or your readers.
- Choose the list of seeds before you look at any results.
- Give the baseline and the new method the same number of runs and the same tuning effort.
- Report the mean, the spread and the number of runs, and say whether the spread is a standard deviation or a standard error.
- Show individual runs when there are only a handful.
- Keep failed or diverged runs in the report and say what happened to them.
The seed is not a hyperparameter to optimize. If you try 50 seeds and keep the best validation score, part of that score is luck that will not carry over to new data. The more seeds you try, the larger the lucky maximum tends to be.
When a deployed system needs one model, choose it by a rule you set in advance, such as the median validation run, or average the predictions of several seeded models. Averaging usually reduces variance, at the cost of more compute to serve.
How many seeds, and what to vary
Three to five seeds will reveal gross instability. Distinguishing small gains needs more, and the number depends on the spread of the paired differences compared with the effect you care about. Halving the standard error takes about four times as many independent runs.
Decide which question you are asking. Varying only the initialization measures how sensitive optimization is on one split. Varying the split as well measures how much the result depends on which data you happened to hold out. The second is usually what someone deciding whether to adopt a method wants to know.
If a single run costs days, you cannot repeat it many times. Measure seed variation on a smaller version of the problem, state that you did so, and treat small differences between single large runs as unresolved. Reporting variance across seeds goes further into presentation.
Common questions
Is there a good seed value?
No value is better than another in principle. Any fixed number works for repeatability.
My two runs differ wildly. Is that normal?
Small differences are expected. Large swings often point to a problem: a learning rate near the edge of divergence, a tiny validation set, a severe class imbalance, or a bug that depends on data order. Start with the checks in debugging a model that will not learn.
Should unit tests use fixed seeds?
Yes, for repeatability. Also make assertions that would hold for most seeds, such as loss falling after training, so the test is not tied to one lucky sequence.