+9 votes
2 views
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)
1
>>> random.randint(1, 10)
3
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
7

2 Answers

+14 votes
by (10.6k points)

  • 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.
+5 votes
by (31.4k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...