Python random.seed() Method



The Python random.seed() method is used to generate the random numbers. It is done with the help of pseudo-random number generator by performing some operation on the given value. The random module creates a random number in Python using the seed value as a base.

A pseudo-random number generator obtains its first "previous" value during seeding. For a specific random number generator, each seed value will correspond to a series of values that were generated. In other words, if you use the same seed twice, you will obtain the same set of numbers twice.

Note − This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.

Syntax

Following is the syntax of Python random.seed() method −

random.seed(x, version)

Parameters

  • x (optional)− This is the seed for the next random number. If omitted, then it takes system time to generate next random number. If the x is None, then by default the current system time is used.

  • version− This is an integer value. It depicts how to convert the parameter x into an integer value. It's default value is 2.

Return Value

This method does not return any value. It only fixes the random number generation.

Example

The following example shows the usage of the Python random.seed() method. Here, we also used another method called random() of the random module. It generates the random number lying between the specified range '10'.

import random
random.seed( 10 )
print ("Random number with seed 10 : ", random.random())
# It will generate same random number
random.seed(10)
print ("Random number with seed 10 : ", random.random())
# It will generate same random number
random.seed(10)
print ("Random number with seed 10 : ", random.random())

When we run above program, it produces following result −

Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469

Example

If we use the same see twice,we will obtain the same set of random numbers twice.

In the example give below the same seed value '98' is provided to the random.seed() method. Hence, the same random number is obtained. This is because the first previous value (seed) is specified for the random number generator. Therefore, in each run, the generator will generate the same number.

# import the random module
import random
# set the seed with the value 98
x = random.seed(98)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)
# again set the seed with the value of 98
x = random.seed(98)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)

While executing the above code we get the following output −

The random number generated is: 35.639820676272215
The random number generated is: 35.639820676272215

Example

In here, we are passing different seed values before calling the random module. Therefore, we obtain different random numbers.

# import the random module
import random
# set the seed with the value 98
x = random.seed(30)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)
# again set the seed with the value of 60
x = random.seed(60)
# generate the random number
print("The random number generated is:", random.random())

Following is an output of the above code −

The random number generated is: 53.90815646058106
The random number generated is: 0.3078067547337774

Example

Following is another example of seed() method using for loop. In here, we see the repeated output 4 times.

import random
for i in range(4):
   # random number with seed 9
   random.seed(9)
   for i in range(3):
      print('The random number with seed 9 is:',random.random())
   print('The random number generated is:')

Output of the above code is as follows −

The random number with seed 9 is: 0.46300735781502145
The random number with seed 9 is: 0.37331193139504204
The random number with seed 9 is: 0.13853941251445523
The random number generated is:
The random number with seed 9 is: 0.46300735781502145
The random number with seed 9 is: 0.37331193139504204
The random number with seed 9 is: 0.13853941251445523
The random number generated is:
The random number with seed 9 is: 0.46300735781502145
The random number with seed 9 is: 0.37331193139504204
The random number with seed 9 is: 0.13853941251445523
The random number generated is:
The random number with seed 9 is: 0.46300735781502145
The random number with seed 9 is: 0.37331193139504204
The random number with seed 9 is: 0.13853941251445523
The random number generated is:
python_numbers.htm
Advertisements