random.seed(): What does it do? random.seed(): What does it do? python python

random.seed(): What does it do?


Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.

Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.

Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.


All the other answers don't seem to explain the use of random.seed(). Here is a simple example (source):

import randomrandom.seed( 3 )print "Random number with seed 3 : ", random.random() #will generate a random number #if you want to use the same random number once again in your programrandom.seed( 3 )random.random()   # same random number as before


>>> random.seed(9001)   >>> random.randint(1, 10)  1     >>> random.seed(9001)     >>> random.randint(1, 10)    1           >>> random.seed(9001)          >>> random.randint(1, 10)                 1                  >>> random.seed(9001)         >>> random.randint(1, 10)          1     >>> random.seed(9002)                >>> random.randint(1, 10)             3

You try this.

Let's say 'random.seed' gives a value to random value generator ('random.randint()') which generates these values on the basis of this seed. One of the must properties of random numbers is that they should be reproducible. When you put same seed, you get the same pattern of random numbers. This way you are generating them right from the start. You give a different seed- it starts with a different initial (above 3).

Given a seed, it will generate random numbers between 1 and 10 one after another. So you assume one set of numbers for one seed value.