How to Use NumPy random seed() in Python

Spread the love

Numpy random seed() in Python is used to generate 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()


# Syntax of Use s() 
numpy.random.seed()

3. Usage of NumPy seed()

The NumPy random() function does not generate ‘truly’ random numbers but we used it to generate pseudo-random numbers.

Random Function

numpy.random.random(size=None)

This function returns a random number in float data type like 0.0, 1.0.

random() is the module offered by the NumPy library in Python to work with random numbers

The above code returns an array of 5 numbers in float type for a seed value of 10.



import numpy as np
np.random.seed(5)
arr = np.random.random(5)
print("The random numbers are:")
print(arr)

# Output :
# The random numbers are:
# [0.22199317 0.87073231 0.20671916 0.91861091 0.48841119]

np.random.seed()
arr = np.random.random(5)
print("The random numbers are:")
print(arr)

# Output :
# The random numbers are:
# [0.68938809 0.08575395 0.63107456 0.24075481 0.74657134]

import numpy as np
np.random.seed(5)
arr = np.random.randint(2,6)
print(arr)




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()

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to Use NumPy random seed() in Python