What exactly is the function of random.seed() in python?

+6 votes

I am a bit confused on what random.seed() does in Python. For example, why does 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)

I couldn't find good documentation on this. Thanks in advance!

Oct 12, 2018 in Python by charlie_brown
• 7,780 points

6 answers to this question.

+2 votes
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.
answered Oct 12, 2018 by aryya
• 7,380 points
+3 votes

The seed method is used to initialize the pseudorandom number generator in Python.

The random module uses the seed value as a base to generate a random number. if seed value is not present it takes system current time. if you provide same seed value before generating random data it will produce the same data. refer  https://pynative.com/python-random-seed/ for more details


import random

random.seed( 30 )
print ("first number  - ", random.randint(25,50))  

random.seed( 30 )
print ("Second number- ", random.randint(25,50))


first number - 42
Second  number - 42

answered Oct 29, 2018 by Rahul

Hey @Rahul.. I get the same output when I run your code from a file but when I run the same code in the python console, I get different output. Can you please tell me why is that so?

You need to run random.seed(30) again to set the seed back to its previous value. Generally, the seed is the previous value generated by the generator. So, when you ran random.randint(25,50) second time, your seed was 42 and not 30. 

If you run random.seed(30) again, 42, 50,...will be the sequence of numbers generated.

Suppose I generate 10 values, will all these values be stored? If yes, where are these values stored? Thanks
No. the last value it generated will be stored because that value will work as the seed for next number
import random

for i in range(2):
    for i in range(20):

Use the above code to see a repeated output for yourself

ohh okayyy! Thanks, the example makes it clearer.
+1 vote
We dont really know
answered Mar 15, 2019 by anonymous
+1 vote

Check this link, but basically the seed is a reference to your random number and when you call it it will generate the same random number


answered Apr 16, 2019 by Yordan Ramshev
+1 vote
Random sequence generation starts with reference to the provided seed. So if same seed provided twice same sequence is obtained as the same algorithm works in background.
answered May 3, 2019 by Abhijit Choudhary
Is it like an XOR function? If you use it twice you get the same output again.
+1 vote

The seed() is one of the methods in Python's random module. It initializes the pseudorandom number generator. You should call it before generating the random number. If you use the same seed to initialize, then the random output will remain the same.

To know the detail, you may refer: Python Random Seed



import random as r

r.seed( 13 )
print("First iteration  - ", r.randint(2, 40))  

r.seed( 13 )
print("Second iteration - ", r.randint(2, 40))


First iteration  -  18
Second iteration -  18
answered Aug 29, 2019 by Vani sharma
That's a really nice explanation. I've upvoted your answer.
What is 13  in seed(13) ??it indicates what

Related Questions In Python

0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
0 votes
1 answer

What is the purpose of hash function in python?

The hash() method returns the hash value of an object if it ...READ MORE

answered Aug 2, 2019 in Python by Mohammad
• 3,210 points
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
+1 vote
1 answer

What is the function for Factorial in Python

Easiest way: math.factorial(x) (available in 2.6 and ...READ MORE

answered Aug 21, 2018 in Python by Priyaj
• 57,700 points

edited Aug 21, 2018 by Omkar 327 views
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 57,700 points
0 votes
1 answer

What is the use of “assert” keyword in Python?

You can try the following in a ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 57,700 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,456 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points