Python Random seed() Method with Examples

random seed() Method in Python:

The random number generator is initialized using the seed() method.

To generate a random number, the random number generator requires a starting number (a seed value).

The random number generator defaults to using the current system time.

To change the random number generator’s starting number, use the seed() method.

Note: Please keep in mind that if you use the same seed value twice, you will get the same random number both times.

How Does the Seed Function Work?
The seed function saves the state of a random function so that it can generate the same random numbers on multiple executions of the code on the same or different machines (for a specific seed value). The previous value number generated by the generator serves as the seed value. When there is no previous value, it uses the current system time for the first time.

Syntax:

random.seed(x, version)

Parameters

x: This is Optional. A seed value is required to generate a random number.
If it is an integer, it is used directly; otherwise, it must be converted to an integer.
The default value is None, and if None is specified, the generator will use the current system time.

version: An integer that specifies how to convert a parameter to an integer.
The default value is 2.

Examples:

Example1:

Input:

Given seed value = 12

Output:

The random number =  0.4745706786885481

Example2:

Input:

Given seed value = 20

Output:

The random number =  0.9056396761745207

Random seed() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the number (seed value) as static input and store it in a variable.
  • Pass the given number as an argument to the random.seed() method to generate a random number, the random number generator requires a starting number (given seed value).
  • Print the random number using the random() function after applying the seed method.
  • The generator generates a random number based on the seed value, so if the seed value is 12, the first random number will always be 0.4745706786885481.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number (seed value) as static input and store it in a variable.
gvn_numb = 12
# Pass the given number as an argument to the random.seed() method to generate
# a random number, the random number generator requires a starting number
# (given seed value).
random.seed(gvn_numb)
# Print the random number using the random() function after applying the seed()
# method.
# The generator generates a random number based on the seed value, so if the seed
# value is 12, the first random number will always be 0.4745706786885481
print("The random number = ", random.random())

Output:

The random number =  0.4745706786885481

If you use the same seed value twice, you’ll get the same random number both times.

import random
random.seed(12)
print(random.random())
random.seed(12)
print(random.random())

Output:

0.4745706786885481
0.4745706786885481

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the number (seed value) as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the random.seed() method to generate a random number, the random number generator requires a starting number (given seed value).
  • Print the random number using the random() function after applying the seed method.
  • The generator generates a random number based on the seed value, so if the seed value is 12, the first random number will always be 0.4745706786885481.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number (seed value) as user input using the int(input()) function 
# and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the random.seed() method to generate
# a random number, the random number generator requires a starting number
# (given seed value).
random.seed(gvn_numb)
# Print the random number using the random() function after applying the seed()
# method.
# The generator generates a random number based on the seed value, so if the seed
# value is 20, the first random number will always be 0.9056396761745207
print("The random number = ", random.random())

Output:

Enter some random number = 20
The random number = 0.9056396761745207