Explore Courses Blog Tutorials Interview Questions
+8 votes
in Python by (1.1k points)
edited by

Please tell me what random.seed() does in Python. For example, why do the below trials do what they do (consistently)?

>>> import random
>>> random.seed(9001)
>>> random.randint(1, 10)
>>> random.randint(1, 10)
>>> random.randint(1, 10)
>>> random.randint(1, 10)
>>> random.randint(1, 10)

2 Answers

+13 votes
by (10.5k points)
edited by

  • random.seed() is used to initialize a pseudo-random number generator in python language.

  • Pseudo-random number generator works by performing operations on a value.

  • random.seed() will give the previous value for a pseudo-random number generator, for the first time it will not give any previous value.

  • The generated values correspond to each seed value and so if you do the same seeding twice, the same sequence of numbers is generated twice.

The seed value is very important as it generates a strong secret encryption key.

Learn python programming from an industry expert, enroll in our Python programming course

+4 votes
by (32.3k points)
edited by

In a simple language, seed method is used to initialize the pseudorandom number generator in Python. The seed value is used as a base by the random module to generate a random number.

Here is a simple example for you:

import random

random.seed( 3 )

print "Random number with seed 3 : ", random.random() #will generate a random number 

#in order to use the same random number once again in your program, just do

random.seed( 3 )

random.random()   # same random number as before

You can use the following video tutorials to clear all your doubts:-

Related questions

Browse Categories