NumPy random seed (Generate Predictable random Numbers) — Like Geeks

Most of you must have to use NumPy random seed during Python coding. Sometimes, we use code repeatedly but don’t exactly know the purpose it serves.

A similar situation is with NumPy random seed. This article is to understand the use of the random seed. And to understand the actual need for random seed and what purpose does it serve.

What is NumPy Random Seed?

As the name signifies, the purpose of random seed is related to random numbers. The syntax mostly used is:

random() is the module offered by the NumPy library in Python to work with random numbers. The NumPy random() function does not generate ‘truly’ random numbers but we used it to generate pseudo-random numbers.

By Pseudo-random numbers we mean, they can be determined, not exactly generated randomly. We will explain pseudo-random numbers in detail in the next section.

The random() function generates pseudo-random numbers based on a seed value.

What is the Pseudo-Random Number?

As the name signifies, the Pseudo-random number is not a ‘truly’ random number but a partial random number. The pseudo-random numbers are computer-generated numbers that look like they are random, but are actually pre-determined.

Our computer system works on algorithms. If we give the same input to an algorithm, the output remains the same.

A set of algorithms created by Computer Scientists to generate pseudo-random numbers, which approximates the properties of random numbers. These algorithms are called “pseudo-random number generators.

NumPy random seed functions generate random numbers based on “pseudo-random number generators “ algorithms.

Random Seed Importance

NumPy random() function generates pseudo-random numbers based on some value. This value is called a seed value.

Numpy.random.seed() method initialized a Random State. Every time this module is called, the generator is re-seeded.

For a specific seed value, the random state of the seed function is saved. So, the particular seed value will produce the same random numbers even on multiple executions.

The same seed value led to the same random number generation even on different machines given the environment remains the same.

With seed value 101, the above random function generates the same output every time.


Here, we can use different seed values. E.g., seed value 100, generates the below output every time for the same random function.


NumPy.random has no Seed Number

Now the question arises what if we don’t give any seed number, then what will happen. Let’s try and execute code with no seed number.

Output on two executions:

We have executed our code twice and the output is different both times. With no seed number, it picks random seeds and different random numbers generated every single time.

Actually, random seed always uses the current system’s time as a seed value when we don’t assign a seed number.


NumPy.random.seed(0) sets the random seed to ‘0’. The pseudo-random numbers generated with seed value 0 will start from the same point every time. NumPy.random.seed(0) is widely used for debugging in some cases.

Output on two executions:

From the above example, in both executions, we got the same set of random numbers with the same seed value ‘0’.


NumPy.random.seed(101) sets the random seed to ‘101’. The pseudo-random numbers generated with seed value ‘101’ will start from the same point every time.

Output on two executions:

From, above example, in both executions, we got the same set of random numbers with seed value 101.

random seed scope

What will happen if we change the random seed scope? Let’s try with an example.


From the above code we see, output of ‘random 1’ and ‘random 2’ are different. Seed value ‘242’ works for ‘random1’ only.

For the ‘random 2’ array, the seed value is picked up randomly. And when we again set the seed value to ‘242’ for ‘random 3’, the same value as of ‘random 1’ comes out.

Seed to the Time

Time never stops. It keeps on moving. Using time as a random seed number is a great idea. Every time we execute the code, the current time changes, so the seed value changes and we get different random numbers on every execution.

Output on two executions:

As we can see from the above example, on both execution different random numbers are generated with the current time as a seed value.

Random Seed Multiprocessing

Multiprocessing is implemented to improve the performance of the system. Every thread executes a different process or we can say multiple processes executed independently.

Imagine, we are implementing multithreading with the same seed value, the output will be the same for every thread. Then what’s the use of running multiple processes. It will be a complete disaster implementation of multiprocessing.

Let’s implement two processes with the same seed value:


From the above example, we can see that we generated the same random number using the same seed value and both processes give the same output.

So, setting random seed values for the different threads is the key. You can do this by explicitly setting different seed numbers for every processor. By doing this, it will randomly pick by itself.


To implement multiprocessing, randomly picking seed value works very well. Process p1 and p2 generate different random numbers, so the output of both processes varies.

Seed the same across computers

NumPy random seed with the same value works similarly across computers. With the same Python version and same operating system Numpy.random.seed() generates the same values across different computers if it takes the same seed value.

Random seed after 1000 time

What happens when we run the same seed more than 1000 times?


I have run numpy.random.seed with seed value ‘100’ for more than 1000 times and pseudo-random values are the same every time.

Random seed 2d array

Using NumPy random function 2D array is generated. With the same seed, the same 2D array with the same random numbers will be generated.


In the above example, we have created a 3*3 size 2D array. After multiple executions, with the same seed, the same array is generated.

How to change random seed?

There are three ways to generate random seed numbers.

  1. The first method is not to pass any seed value. It will randomly pick seed value by itself as we describe in the section above in detail.
  2. The second way is to pass the current time as seed number. Time is always changing, so a random seed number will be generated.
  3. The third way is to randomly generate seed numbers using random.randint(). See the example below.

Output on two execution:

On every execution, it generates a new seed value, so that generates a different set of pseudo-random numbers.

NumPy random seed shuffle

You can shuffle the sequence of numbers using NumPy random.shuffle(). Using shuffle without using seed, it shuffles the sequence randomly, every time we execute the command.

With the same seed value, you can shuffle the sequence in a particular order, every time we execute the command.


In the above code, using the seed() function with the same value, every execution results in the same value as shown above.

Without using the seed() function it shuffles randomly on every execution.

NumPy random seed vs Python random seed

There are two ways to initialize seed. One is using the Python pseudo-random generator random.seed() like this:


The second method is using NumPy pseudo-random generator np.random.seed() like this:


Both functions work on pseudo-random generator algorithms internally. But, with the same seed value, both functions give different output starting from different random values.


Random number generations are very crucial and important in various fields like probability, statistics, machine learning, and deep learning applications. We have discussed all major functions and scenarios of random.seed() function.

The practice is the key to a deep understanding of any topic. Keep experimenting with code snippets I shared in the article. The more you practice, the clearer the topic will be.

Originally published at on February 12, 2021.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store