Python random seed()

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The Python random seed() method is found in Python's built-in module random. It is used to initialize the pseudo-random number generator of Python which generates the random values. The Python random seed() method takes two parameters in which the first parameter (default value = None) is an optional parameter that is used to provide the seed value for the random number generator. The second parameter (default value = 22) is required which is used to tell Python's random generator how the conversion of parameters into an integer value is to be performed. The method returns None, it only fixes the generation of the random values.

Syntax of Python Random seed()

The Python random seed() method is used to initialize the pseudo-random number generator of Python which will generate random numbers.

The syntax of the random sample Python is very easy.

Here, the seedValue is an optional parameter which denotes the seed value for the random number generator. The version parameter is an integer which depicts how the conversion of parameter into an integer values is to be performed.

The value of the seed is set to version.

Refer to the next section for more details about the parameters of the Python random seed() method.

Parameters of Python Random seed()

The Python random seed() method takes two parameters in which the first parameter is an optional parameter but the second parameter is a required parameter.

  • seedValue: The seedValue parameter of the Python random seed() method is optional. It is used to set the seed value needed to generate a random number. The seedValue parameter is supposed to be an integer. If the seedValue is an integer then it can be directly used else, the parameter needs to be converted into an integer.

    • The default value of the seedValue parameter is set to None. If we do not provide any seedValue then it uses None which tells the random generator to use the current system time.
  • version: The version parameter of the Python random seed() method is a required parameter. The version parameter needs to be an integer. The version parameter tells the Python interpreter how the conversion of the seedValue (first parameter) into an integer need to be performed.

    • The default value of the version parameter is set to be 2.

Return Values of Python Random seed()

The Python random seed() method returns nothing (None). It only fixes the generation of random numbers. The only aim of the Python random seed() method is to initialize the Python random number generator.

Exceptions of Python Random seed()

Usually, the Python random seed() method does not raise an error if we use the correct syntax.

We can provide the version parameter in any data type such as integer, string, floating number, etc.

Example of Python Random seed()

Let us now take an example and see how the Python random seed() method works. We are also using another function called randint() of the random module which generates a random number lying between the specified range (specified in the randint() parameter).

Output in the first run:

Output in the second run:

Output in the third run:

As we can see that if we specify or set the seed with a certain value it will generate the same value in all the executions of the program.

What is Python Random seed()?

We use various functions of the random module to generate random numbers, choices, and sequences. The random number generator generates the random numbers in Python. But in many cases, we want that the random number generator to generate the same number(s) in each run for all the machines. So, we use the Python random seed() method.

The Python random seed() method is used to initialize the pseudo-random number generator of Python which generates the random values.

The Python random seed() method takes two parameters in which the first parameter is an optional parameter which is used to provide the seed value for the random number generator. The second parameter is used to tell Python's random generator how the conversion of parameters into an integer value is to be performed.

The default value of the first optional parameter is set to None. On the other hand, the default value of the second required parameter is set to 22. The first parameter is supposed to be an integer. If the first parameter is an integer then it can be directly used by Python's random number generator else, the parameter needs to be converted into an integer. Now, if None is provided as the first parameter of the Python random seed() method then it tells the Python's random generator to use the current system time.

The Python random seed() method returns None, it only fixes the generation of the random values.

How Seed Function Works?

As we have seen that the Python random seed() method is used to initialize the Python random number generator. The Python random number generator performs some operations by working on a value. Now, this value is usually the previously generated value by the generator. In the first execution, there is no specified previous value.

So, we use the Python random seed() method to set the first previous value for the Python random number generator. Now, if we have specified the first last value (seed) for the random number generator, the generator will always generate the same number in each run.

As we know that if we do not specify a seed value, the Python random number generator uses None as the seed value which resembles the current time.

So, in simple terms, we can say that the Python random seed() method initializes the Python random number generator and sets the first previous value for the generator so that the generator generates the same set of value(s) in every run.

Note: The Python random seed() method saves the state of the Python random function so that every execution results in the same set of values.

More Examples

Let us take a few examples to understand the working of the Python random seed() method for a better understanding.

If You Use the Same Seed Value Twice, You Will Get the Same Random Number Twice

Suppose that we provide the same seed value twice then let us see what will happen.

Output:

As we can see that if we have provided the same seed value to the Python random seed() method, we are getting same random number.

The reason for the generation of the same random number is that we have specified the first previous value (seed) for the random number generator so the generator will always generate the same number in each run.

You Want Different Data, then Pass the Different Seed Value before Calling any other Random Module Function

Now, if we want different random values by the various random module methods like randint() and random() etc., we can pass the different seed value before calling any other random module function. Let us see the code for better understanding.

Output:

As we can see that if we have specified different seed value before calling a random module's function, we get different random numbers.

Uses of random.seed()

In various scenarios, we want that the random number generator to generate same number(s) in each run for all the machines. So, we use the Python random seed() method to initialize the pseudo-random number generator of Python which generates the random values.

Now let us see some of the use cases of the Python random seed() method.

  • The Python random seed() method is used to generate the pseudo-random encryption key. As we know, encryption keys play a vital role in computer and application security. So, to generate the secret pseudo-random encryption key, we can use the Python random seed() method.
  • The Python random seed() method is widely used in the testing field. Since the output code is dependent on the input, we can provide the same set of random input values to the various programs for testing.

Conclusion

  • The Python random seed() method is found in the Python's built-in module random.
  • The Python random seed() method is used to initialize the pseudo-random number generator of Python which generates the random values.
  • The Python random number generator performs some operations by working on the previously generated value by the generator.
  • In the first execution, there is no specified previous value. So, we use the Python random seed() method to set the first previous value for the Python random number generator.
  • The Python random seed() method takes two parameters. The syntax of the method is: random.seed(seedValue, version)
  • The first parameter is an optional parameter that is used to provide the seed value for the random number generator.
  • The default value of the first optional parameter is set to None.
  • The second parameter is used to tell Python's random generator how the conversion of the parameter into an integer value is to be performed.
  • The default value of the second required parameter is set to 22.
  • The Python random seed() method returns None, it only fixes the generation of the random values.

See Also:

To understand more about Python programming language and important Python functions, refer to: