Numpy random seed – Python NumPy random.seed() Function

Numpy random seed: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.seed() Function:

Numpy random seed: To seed the generator, use the NumPy random.seed() function. This method is invoked when RandomState is initialized. It is possible to re-seed the generator by calling it again.

Syntax:

numpy.random.seed(seed=None)

Parameters

seed: This is Optional. It Specifies seed for RandomState. Convertible to 32-bit unsigned integers are required. int or 1-dimensional array like.

Working of the Seed Function

np.random.seed: The seed function saves the state of a random function so that it can create the same random numbers on repeated executions of the code on the same or other machines (for a specific seed value). The previous value number created by the generator serves as the seed value. When there is no previous value, it uses the current system time for the first time.

Pseudo-Random Number

Random seed numpy: It is a number. A number that appears to be random(Pseudo-random)

So, in essence, a pseudo-random number is a number that is nearly random but not truly random.

It may appear to be sarcastic here, but that’s exactly what they are. Pseudo-random numbers are numbers that look to be random but are not.

A pseudo-random number is defined as follows by Wolfram Mathworld’s encyclopedia:

A computer-generated random number.

The prefix pseudo distinguishes this type of number from a “truly”  generated random number by a random physical process such as radioactive decay.

Applications of Seed Function

Python seed: This is used to create a pseudo-random encryption key. Encryption keys are a critical component of computer security. These are the types of secret keys that are used to safeguard data against unwanted internet access.

It simplifies code optimization in situations when random numbers are needed for testing. The code’s output is sometimes dependent on the input. As a result, using random numbers to evaluate algorithms can be complicated. Also, the seed function is used to create the same random integers over and over again, which simplifies the algorithm testing procedure.

NumPy random.seed() Function in Python

Example1

Here, the random.seed() function is used to seed the generator. When seed is provided, the outcome is preserved, making it possible to reproduce the result.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random number to the random.seed() function to seed the generator.
  • Get the pseudo normally distributed random numbers by passing the size (rowsize, columnsize) as an argument to the numpy.random.normal() function.
  • Store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random number to the random.seed() function to seed the generator
np.random.seed(10)

# Get the pseudo normally distributed random numbers by passing the size
# (rowsize, columnsize) as argument to the numpy.random.normal() function.
# Store it in a variable.
rslt = np.random.normal(size=(3,3))
# Print the above result.
print(rslt)

Output:

[[ 1.3315865 0.71527897 -1.54540029]
 [-0.00838385 0.62133597 -0.72008556]
 [ 0.26551159 0.10854853 0.00429143]]