What is the Seed() Function in NumPy?
The seed() function in NumPy is used to set the random seed of the NumPy pseudo-random number generator. It offers a crucial input that NumPy needs to produce pseudo-random integers for random processes and other applications.
By default, the random number generator uses the current system time. We need to call the seed() function before calling any other random module function. To get a better understanding of the seed() function, let's understand what pseudo-random numbers are in Numpy.
Introduction to Pseudo-Random Numbers
-
Pseudo-Random Numbers Appear to Be Random
As the name suggests, pseudo-random numbers are a set of partially random numbers. They appear to be random but are not random. According to Wikipedia, a pseudo-random number is a computer-generated random number. It cannot be random because it was generated by a computer; instead, it is predetermined. To generate a seemingly random number, various computations are made. Therefore, we refer to it as partially random. Let's examine why NumPy requires pseudo-random numbers.
-
Pseudo-Random Numbers are Generated by Algorithms When we work on software, testing it is an integral part of its development, as we need the software to be robust and handle any errors that happen. The problem with that is what happens when we use computers/applications to work with random processes. Since computers are deterministic (not random), they will follow some instructions and produce an output. If we give a computer the same inputs, it will provide the same outputs over and over again. Hence, we will never be able to generate random numbers. To overcome this problem, we create pseudo-random numbers, which in turn are created using algorithms. The essence of computers (being deterministic) can never be changed, but pseudo-random numbers produce numbers that approximate the random numbers.
-
Pseudo-Random Numbers Can Be Re-Created Exactly Because of np random seed values, we can re-create random numbers according to our liking. When we set an np random seed value, the values returned by the random() function will always be constant. As we set an np random seed value, the algorithm that generates random values takes in the np random seed value. This ensures that the values remain constant throughout.
Even if we run the code a lot of times, the random value returned would always be the same.
Output
Why We Need Pseudo-Random Numbers
As computers are deterministic, they cannot generate random values intrinsically, as to generate random values, we need the system to be random. With the help of pseudo-random numbers, we can generate random values as the np random seed value goes into an algorithm.
This algorithm generates a random number (approximately a random number), which lies between 0 and 1.
Syntax Expounded
The syntax for seed() function in NumPy is:
Its parameters are:
- val: This is the input seed value to generate repeated random numbers. It's a mandatory parameter.
NumPy Random Seed Functions
-
Random Function As discussed earlier in this article, the pseudo-random numbers help us to get the same set of random numbers every time we call the seed() function. This is possible because we set the np random seed value as a specific number, and NumPy then processes that number and gives us the random numbers.
Output
As we can see, a float value is generated. It's between 0 and 1. If we run the code again, we will see that the same value will be returned. This happens because we set our np random seed as 0. If we change the np random seed value, then the random numbers will also be changed.
-
Random Integer Function
To generate random integers in NumPy, we use the numpy.random.randint function.
Output
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.
-
Random Shuffle Function
We'll change the sequence of an array using the shuffle function in NumPy.
Output
-
Random Standard Exponential Function
The standard exponential function helps us to draw samples from the standard exponential distribution. It takes in two parameters; size (The shape of the output) and out (The samples)
Output
As we can see, a set of random samples are returned when we use the standard_exponential() function.
-
Random Triangular Function
The triangular function in NumPy helps us to generate a random value between two data points, and it also has a bias parameter, which can be used to create a random value near any of the two data points.
Output
As we can see, we've got 19 as our generated random number. This happened because we set our bias (mode) as 20. Hence we got a number closer to our mode (20).
Use of NumPy Random Seed
Some of the applications of the Random Seed function in NumPy are:
- NumPy Random Seed function is used in creating encryption keys in computer and network security. This is done because of pseudo-generated random numbers that we talked about earlier in this article. These keys are secret keys and are important to protect against the misuse of data over the internet.
- In the software lifecycle, an integral part of it is software testing. The random Seed function helps us to make codes optimized where we use random numbers for testing. When we use random numbers and values to test our data, it makes our software robust, as it understands and comprehends different data and use cases.
Conclusion
In this article, we discovered the seed() function and how we use it to generate random numbers in NumPy. We understood how computers, being completely deterministic, end up generating random numbers with the help of pseudo-random numbers.
Apart from that, we also covered the following:
- We need pseudo-random numbers as we can create random values by using pseudo-random integers as the algorithm's np random seed value.
- We learned about some utility functions of np random seed generation like rand() (helps us to generate a random number), randint() (helps us to generate a random integer), and shuffle() (helps us to randomize the sequence of an array).