Skip to main content

How to Get the Same Results Every Run with random_state in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
train_test_split(<X>, <y>, random_state=<whole number>)
<Model>(random_state=<whole number>)
PartWhat 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=NoneThe 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​

seeds.py
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}")

The seeds.py script in a macOS-style code window: for seed in None and 0, it splits the diabetes data and trains a 50-tree RandomForestRegressor with random_state=seed, then prints the R2 score.

Step 2: Run It Twice​

Terminal
uv run python seeds.py
Output (first run)
random_state=None: R2 0.3919
random_state=0: R2 0.2046

Run the same command again:

Output (second run)
random_state=None: R2 0.4183
random_state=0: R2 0.2046

Two runs of seeds.py side by side: random_state=None scored 0.3919 and then 0.4183, while random_state=0 scored 0.2046 both times.

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​

MistakeWhat happensDo this instead
Fixing random_state on the split but not on the modelThe test set is stable, but the model still changes between runsSet it on every object that takes one
Picking the seed that gives the best scoreThe score looks better than the model really isChoose a seed once and keep it

Troubleshooting​

SymptomLikely causeFix
Scores change between runs of the same scriptSome random_state is still None, the defaultPass 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​