---
title: "How to set the global random_state in Scikit Learn"
description: "What to do if you keep forgetting to set the random_state?"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/how-to-set-the-global-random-state-in-scikit-learn
---

Such information should be in the first paragraph of Scikit Learn manual, but it is hidden somewhere in the FAQ, so let’s write about it here.

Scikit Learn does not have its own global random state but uses the numpy random state instead. If you want to have reproducible results in Jupyter Notebook (you should want that ;) ), set the seed at the beginning of your notebook:

```
np.random.seed(31415)
```

How can we check if it works? Run this code:

```
import numpy as np
print('Without seed')
print(norm.rvs(10, size = 4))
print(norm.rvs(10, size = 4))

print('With the same seed')
np.random.seed(31415)
print(norm.rvs(10, size = 4))
np.random.seed(31415) # reset the random seed back to 31415
print(norm.rvs(10, size = 4))

print('Without seed')
np.random.seed(None)
print(norm.rvs(10, size = 4))
print(norm.rvs(10, size = 4)
```

In my case the output was:

```
Without seed
[11.87381912 10.67665352 10.93843519  9.68574986]
[10.16669138  9.41330164  9.64055638  8.49694282]
With the same seed
[11.36242188 11.13410818 12.36307449  9.74043318]
[11.36242188 11.13410818 12.36307449  9.74043318]
Without seed
[ 8.79608103  9.40920579 11.23146236 10.18055655]
[11.5560791   9.77978961 11.9580387  11.39481905]
```