How to use random.seed() function In Python
Here we will see how we can generate the same random number every time with the same seed value.
Example 1:
Python3
# random module is imported import random for i in range ( 5 ): # Any number can be used in place of '0'. random.seed( 0 ) # Generated random number will be between 1 to 1000. print (random.randint( 1 , 1000 )) |
865 865 865 865 865
Example 2:
Python3
# importing random module import random random.seed( 3 ) # print a random number between 1 and 1000. print (random.randint( 1 , 1000 )) # if you want to get the same random number again then, random.seed( 3 ) print (random.randint( 1 , 1000 )) # If seed function is not used # Gives totally unpredictable responses. print (random.randint( 1 , 1000 )) |
244 244 607
On executing the above code, the above two print statements will generate a response 244 but the third print statement gives an unpredictable response.
random.seed( ) in Python
random() function is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random() function generates numbers for some values. This value is also called seed value.
Syntax : random.seed( l, version )
Parameter :
- l : Any seed value used to produce a random number.
- version : A integer used to specify how to convert l in a integer.
Returns: A random value.