How to Get the Same Results Every Run with random_state in scikit-learn
Run the same scikit-learn script twice and you can get two different scores, because splitting the data and building some models involve random choices. Passing a whole number as random_state fixes those choices, so every run gives the same result. This guide runs one script twice, with and without it, on scikit-learn 1.9.1.
Syntax
train_test_split(<X>, <y>, random_state=<whole number>)
<Model>(random_state=<whole number>)
| Part | What it means |
|---|---|
random_state=<whole number> | Fixes the random choices: the same number gives the same split, or the same model, every time |
random_state=None | The default: new random choices on every run |
scikit-learn's guide to controlling randomness says: "If an integer is passed, calling fit or split multiple times always yields the same results." and "For reproducible results across executions, remove any use of random_state=None."
The Example
The script splits the diabetes dataset and trains a random forest on it twice: once with random_state=None and once with random_state=0. It prints the R² score of each. Then it runs again, to compare the two runs.
Prerequisites
- scikit-learn in your project, for example with
uv add scikit-learn. This guide uses scikit-learn 1.9.1 on Python 3.15.2.
Step 1: Write the Script
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
X, y = load_diabetes(return_X_y=True)
for seed in (None, 0):
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=seed)
model = RandomForestRegressor(n_estimators=50, random_state=seed)
model.fit(X_train, y_train)
print(f"random_state={seed}: R2 {model.score(X_test, y_test):.4f}")

Step 2: Run It Twice
uv run python seeds.py
random_state=None: R2 0.3919
random_state=0: R2 0.2046
Run the same command again:
random_state=None: R2 0.4183
random_state=0: R2 0.2046

With random_state=None, the score moved from 0.3919 to 0.4183, because the split and the forest were different each time. With random_state=0, both runs gave 0.2046. The command works unchanged in PowerShell.
The value of the score doesn't matter here. 0 gave a lower score because a different set of patients landed in its test set; it isn't a better or worse seed. What matters is that it's the same every run.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Fixing random_state on the split but not on the model | The test set is stable, but the model still changes between runs | Set it on every object that takes one |
| Picking the seed that gives the best score | The score looks better than the model really is | Choose a seed once and keep it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Scores change between runs of the same script | Some random_state is still None, the default | Pass a whole number everywhere |
FAQ
Which number should I use?
Any whole number. What matters is using the same one every time; 0 and 42 are common choices.
Does every model use random_state?
Only the ones with random steps. When a model has the parameter, set it, so the code stays reproducible even if a later change, such as more data, makes the model use randomness.
Conclusion
The same script scored 0.3919 and then 0.4183 with random_state=None, and 0.2046 both times with random_state=0. Pass a whole number to every split and model that takes one, and keep it.
Additional Resources
- Controlling randomness — scikit-learn's guide.
- random_state in the glossary — what each kind of value does.
