np.random.seed(0) makes the random numbers predictable

>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])

With the seed reset (every time), the same set of numbers will appear every time.

If the random seed is not reset, different numbers appear with every invocation:

>>> numpy.random.rand(4)
array([ 0.42,  0.65,  0.44,  0.89])
>>> numpy.random.rand(4)
array([ 0.96,  0.38,  0.79,  0.53])

(pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.

If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it.

To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock.

For more information on using seeds to generate pseudo-random numbers, see wikipedia.

Answer from John1024 on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.2 › reference › random › generated › numpy.random.seed.html
numpy.random.seed — NumPy v2.2 Manual
Reseed the singleton RandomState instance · This is a convenience, legacy function that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module
Top answer
1 of 12
890

np.random.seed(0) makes the random numbers predictable

>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])

With the seed reset (every time), the same set of numbers will appear every time.

If the random seed is not reset, different numbers appear with every invocation:

>>> numpy.random.rand(4)
array([ 0.42,  0.65,  0.44,  0.89])
>>> numpy.random.rand(4)
array([ 0.96,  0.38,  0.79,  0.53])

(pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.

If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it.

To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock.

For more information on using seeds to generate pseudo-random numbers, see wikipedia.

2 of 12
78

If you set the np.random.seed(a_fixed_number) every time you call the numpy's other random function, the result will be the same:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10) 
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]

However, if you just call it once and use various random functions, the results will still be different:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10)
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> print np.random.permutation(10) 
[3 5 1 2 9 8 0 6 7 4]
>>> print np.random.permutation(10) 
[2 3 8 4 5 1 0 6 9 7]
>>> print np.random.rand(4) 
[0.64817187 0.36824154 0.95715516 0.14035078]
>>> print np.random.rand(4) 
[0.87008726 0.47360805 0.80091075 0.52047748]
Discussions

Is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the RNG)?
That's how a seed works. Its the starting point, which is why it's called the "seed". Every random number generated will also update the seed to be used in the generation of the next random number. So yea, to do what you want you need to reset it inside the loop. I'm missing why you don't want to do that? More on reddit.com
🌐 r/learnpython
12
8
April 11, 2024
python - How to set the fixed random seed in numpy? - Stack Overflow
I was believing that setting a seed always gives the same result. But I got different result each times.How to set the seed so that we get the same result each time? Here is the MWE: import numpy... More on stackoverflow.com
🌐 stackoverflow.com
Is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the RNG)?
You do *not* want to set the seed every time you call the RNG. This literally destroys the utility of the RNG. Show us your code. More on reddit.com
🌐 r/learnpython
8
3
November 10, 2019
Explain random.seed() like I’m five.
Back before computers, they used to publish books of random numbers . random.seed() is like opening a book of random numbers to a specific page. The numbers on that page are still random, but if you remember what page you turned to you will always get the same random numbers. In python, this means your code will consistently produce the same results every time. This is useful if, for example, you a writing a tutorial for random.randint() and you want to have text that references the numbers that the function spits out (along with many types of analyses that involve randomness). If you don't care about having consistent results each time you run your code, you don't need to use it. More on reddit.com
🌐 r/learnpython
22
54
January 17, 2022
🌐
Built In
builtin.com › data-science › numpy-random-seed
NumPy Random Seed: How It Works and Why to Stop Using It | Built In
A NumPy random seed is a numerical value in Python that sets the starting state for generating random numbers, ensuring reproducible results. Here's why to use np.random.default_rng().
🌐
Reddit
reddit.com › r/learnpython › is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the rng)?
r/learnpython on Reddit: Is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the RNG)?
April 11, 2024 -

OK, I got the exactly same problem as it was posted 4 years ago in the sub. https://www.reddit.com/r/learnpython/comments/du4f22/is_there_a_way_to_set_the_seed_for_numpyrandom/, but it seems to be not answered directly (the usual "you shouldn't do this" stuff).

Specifically about my problem, I am writing this GUI that down-samples the data for visualization. The "down sampling" is just to avoid slowing down the matplotlib plotting engine. I want the user to see the same plot every time they look at the data with the same settings / input.

Code here:

import numpy as np
import matplotlib.pyplot as plt
import time

# Generate fake data
x = np.random.normal(size=2000)
y = x * 2 + np.random.normal(size=2000)

sampleRNG = np.random.default_rng(42)

# The for loop to simulate user inputing differnt down-sample settings
for down_sample_size in [500, 600, 200, 500, 700]:

    # problem solved if the following line is not commented out
    # sampleRNG = np.random.default_rng(42)
    
    randIndex = sampleRNG.choice(len(x), size=down_sample_size, replace=False, axis=0, shuffle=False)
    
    fig, ax = plt.subplots()
    ax.scatter(x[randIndex], y[randIndex], s=2)
    plt.show()
    
    time.sleep(1)

Note, the "for loop" in the above code is just for mimicking users changing the down-sample settings to look at the data again. My goal is to have the first and forth plot (both with a down-sample size at 500) looks the same. This will be resolved if I put the sampleRNG = np.random.default_rng(42) inside the loop, but I am wondering if this is the right thing to do.

Method I have considered to get around:

  1. Shuffle the sample when it's loaded, and only take the first N points of the sample when user requires to down sample to N. This is like pre-rendering the data, but it will take double the memory, as I do want to keep the original data order.

Feedback is welcome....

🌐
Medium
medium.com › data-science › random-seeds-and-reproducibility-933da79446e3
Random Seeds and Reproducibility. Setting Up Your Experiments in Python… | by Daniel Godoy | TDS Archive | Medium
May 14, 2022 - There is no such thing, but we can try the next best thing: our own function to set as many seeds as possible! The code below sets seeds for PyTorch, Numpy, Python's random module, and the sampler's generator; besides configuring PyTorch's backend to make CUDA convolution operations deterministic.
🌐
Scaler
scaler.com › home › topics › what is the seed() function in numpy?
What is the Seed() Function in NumPy? - Scaler Topics
January 5, 2023 - We set the np random seed value as 4 in this case. The parameters low and high help us to set the limits of our random values, and size helps us to select the number of elements that we want. ... We'll change the sequence of an array using the shuffle function in NumPy.
🌐
Medium
medium.com › @heyamit10 › why-use-numpy-random-seed-44fd89a8c3d7
Why Use numpy.random.seed()?. If you think you need to spend $2,000… | by Hey Amit | Medium
February 8, 2025 - Here’s the deal: when you set a seed using numpy.random.seed(), you tell Python to “stick to this starting point.”
Find elsewhere
🌐
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.random.seed.html
numpy.random.seed — NumPy v1.15 Manual
Seed the generator. This method is called when RandomState is initialized. It can be called again to re-seed the generator.
🌐
Like Geeks
likegeeks.com › home › python › numpy › numpy random seed (generate predictable random numbers)
NumPy random seed (Generate Predictable random Numbers)
October 17, 2023 - Here’s a code snippet that shows best practices: import numpy as np # Set the seed early seed_value = 42 np.random.seed(seed_value) random_numbers = np.random.rand(3) print(f"Seed: {seed_value}") print(f"Random Numbers: {random_numbers}")
🌐
Analytics Vidhya
analyticsvidhya.com › home › what does numpy.random.seed() do?
What Does numpy.random.seed() Do? - Analytics Vidhya
April 4, 2025 - In the next section, you understand well what this means when you learn it with python code. The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers.
🌐
NumPy
numpy.org › doc › stable › reference › random › generated › numpy.random.seed.html
numpy.random.seed — NumPy v2.4 Manual
Reseed the singleton RandomState instance · This is a convenience, legacy function that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module
🌐
Posit
keras3.posit.co › reference › set_random_seed.html
Sets all random seeds (Python, NumPy, and backend framework, e.g. TF). — set_random_seed • keras3
Note that the TensorFlow seed is set even if you're not using TensorFlow as your backend framework, since many workflows leverage tf$data pipelines (which feature random shuffling). Likewise many workflows might leverage NumPy APIs. ... Integer, the random seed to use. No return value, called for side effects. https://keras.io/api/utils/python_utils#setrandomseed-function
🌐
Delft Stack
delftstack.com › home › howto › numpy › python numpy random seed
How to numpy.random.seed() Function in NumPy | Delft Stack
March 11, 2025 - Learn how to effectively use the numpy.random.seed() function in NumPy for reproducible random number generation in Python. This guide covers basic usage, generating random integers, and creating samples from a normal distribution, ensuring your results remain consistent across different runs.
🌐
Quora
quora.com › How-does-one-set-a-randon-seed-in-Numpy-according-to-the-current-time
How does one set a randon seed in Numpy according to the current time? - Quora
Numeric is like NumPy a Python module for high-performance, numeric computing, but it is obsolete nowadays. Another predecessor of NumPy is Numarray, which is a complete rewrite of Numeric but is deprecated as well. NumPy is a merger of those two, i.e. it is built on the code of Numeric and the features of Numarray. ... Enable seamless and secure communication for everyone with Cisco collaboration solutions from CDW. ... The best method is probably random.seed() (with no argument or with None as the argument).
🌐
NumPy
numpy.org › devdocs › reference › random › generated › numpy.random.seed.html
numpy.random.seed — NumPy v2.5.dev0 Manual
Reseed the singleton RandomState instance · This is a convenience, legacy function that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to use numpy random seed() in python
How to Use NumPy random seed() in Python - Spark By {Examples}
March 27, 2024 - The NumPy random seed() function is used to seed the random number generator in NumPy. Seeding the random number generator allows you to get reproducible
🌐
NumPy
numpy.org › doc › 2.0 › reference › random › generated › numpy.random.seed.html
numpy.random.seed — NumPy v2.0 Manual
Reseed the singleton RandomState instance · This is a convenience, legacy function that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module
🌐
NumPy
numpy.org › doc › 2.1 › reference › random › generated › numpy.random.seed.html
numpy.random.seed — NumPy v2.1 Manual
Reseed the singleton RandomState instance · This is a convenience, legacy function that exists to support older code that uses the singleton RandomState. Best practice is to use a dedicated Generator instance rather than the random variate generation methods exposed directly in the random module