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.
How Seed Function Works ?
Seed function is used to save the state of random function, so that it can generate some random numbers on multiple execution of the code on the same machine or on different machines (for a specific seed value). Seed value is the previous value number generated by the generator. For the first time when there is no previous value, it uses current system time.
Using random.seed() function –
Here we will see how we can generate same random number every time with same seed value.
Code #1:
# 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
Code #2:
# 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 response. 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.
Uses of random.seed() –
- This is used in generation of pseudo-random encryption key. Encryption keys are important part of computer security. These are the kind of secret keys which used to protect data from unauthorized access over internet.
- It makes optimization of codes easy where random numbers are used for testing. The output of the code sometime depends on input. So the use of random numbers for testing algorithm can be complex. Also seed function is used to generate same random numbers again and again and simplifies algorithm testing process.
Recommended Posts:
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Any & All in Python
- max() and min() in Python
- gcd() in Python
- pow() in Python
- Use of min() and max() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



