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 results when generating random numbers. This function generates pseudo-random numbers based on a seed value. A pseudo-random number is a number that sorts random, but they are not really random numbers.

The NumPy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the NumPy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers. When the value is not mentioned in the NumPy random seed, then the computer will use the current system time in seconds or milliseconds as a seed value to generate a different set of 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.

## 1. 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.

## 2. Syntax of NumPy random.seed()

Following is the Syntax of np.random.seed().

```
# Syntax of Use seed()
numpy.random.seed(seed=None)
```

### 2.1 Parameters of random.seed()

Following are the parameters of seed() function.

`seed`

– This is an optional parameter. It is an integer or an array_like object that is used to seed the random number generator. If `seed`

is not provided or is `None`

, the generator is initialized based on the system time or entropy source, which means you’ll get different sequences of random numbers each time you run your program. If you provide a specific integer value as the seed, you’ll get the same sequence of random numbers every time you run your program with that seed.

## 3. Usage of NumPy seed()

The `numpy.random.seed()`

function is used to seed the random number generator in NumPy. Seeding is important for reproducibility. By setting the seed, you ensure that the sequence of random numbers generated by NumPy is the same every time you run your code with that seed.

In the below example, `np.random.seed(5)`

sets the seed for NumPy’s random number generator to the integer value 5. Setting the seed is crucial for reproducibility. If you run this code multiple times, you will get the same sequence of random numbers each time because the seed is fixed. `np.random.random(5)`

generates an array of 5 random numbers from a uniform distribution between 0 (inclusive) and 1 (exclusive). The result is assigned to the variable `arr`

.

```
# Import numpy
import numpy as np
# Generate random numbers
np.random.seed(5)
arr = np.random.random(5)
print("The random numbers are:\n",arr)
```

Yields below output.

In the below example,` np.random.seed()`

sets the seed for NumPy’s random number generator based on the system time or some other entropy source. When no seed value is provided within the parentheses, the seed is set dynamically each time the code is executed. As a result, you’ll get different sequences of random numbers each time you run your program. `np.random.random(5)`

generates an array of 5 random numbers from a uniform distribution between 0 (inclusive) and 1 (exclusive). The result is assigned to the variable `arr`

.

```
# Generate random numbers
np.random.seed()
arr = np.random.random(5)
print("The random numbers are:\n",arr)
# Output:
# The random numbers are:
# [0.27711177 0.10360379 0.44506416 0.27977484 0.85190227]
```

## 4. Use numpy. random.seed() Function

Use the `np.random.seed()`

function to set the seed for the random number generator. For instance, First, import the NumPy library and use the alias `np`

. Set the seed to a specific value, in this case, 42. You can replace 42 with any integer. Generate a 3×3 array of random numbers from a uniform distribution between 0 and 1.

```
# Set the seed to a specific value
np.random.seed(42)
# Generate random numbers
arr = np.random.rand(3, 3)
print("The random numbers are:\n",arr)
# Output:
# The random numbers are:
# [[0.37454012 0.95071431 0.73199394]
# [0.59865848 0.15601864 0.15599452]
# [0.05808361 0.86617615 0.60111501]]
```

## 5. Graphical Representation of NumPy random.seed() Function

To make a histogram with the help `pyplot`

library and print the graph of NumPy `random.seed()`

function.

```
import numpy as np
import matplotlib.pyplot as plt
new_samp = np.random.choice(16, 3000)
num, b, i = plt.hist(new_samp, 21, density = True)
plt.show()
```

## Frequently Asked Questions

**What does numpy.random.seed() do?**

`numpy.random.seed()`

is used to seed the random number generator in NumPy. Seeding the generator ensures that if you use the same seed, you’ll get the same sequence of random numbers every time you run your code. This is crucial for reproducibility.

**Why is seeding important in random number generation?**Seeding is important because it allows you to generate the same sequence of random numbers, making your results reproducible. It’s particularly useful in scientific experiments, simulations, and data analysis where you want to ensure that your results are consistent across runs.

**Does setting the seed affect all random functions in NumPy?**Setting the seed with `numpy.random.seed()`

affects all subsequent calls to random number generation in NumPy within the same program. It ensures consistency across all random functions.

**What happens if I don’t set the seed?**If you don’t set the seed using `numpy.random.seed()`

in NumPy, the random number generator is initialized based on the system time or another entropy source. This means that every time you run your program, you’ll get a different sequence of random numbers.

**Can I use any integer as a seed value?**You can use any integer as a seed value. The key is to use the same seed if you want the same sequence of random numbers. The actual value of the seed doesn’t matter; it’s the consistency that matters for reproducibility.

**Can I use numpy.random.seed() multiple times in a program?**

You can use `numpy.random.seed()`

multiple times in a program to set the seed at different points. Each time you set the seed, it affects the subsequent calls to random number generation.

### Related Articles

- How to Use NumPy Exponential Function
- How to get Diagonal of NumPy Array Using diag()
- How to do matrix multiplication in NumPy?
- Python NumPy floor() Function
- How to Use NumPy log() in Python
- How to Use Numpy random.rand() in Python
- How to Use NumPy random.uniform() in Python
- How to Use NumPy Exponential Function
- How to Use NumPy random.randn() in Python?
- How to Use NumPy random.normal() In Python?