Python provides seed()
function from the random module that is used to set the seed value to generate pseudo-random numbers. A pseudo-random number is a number that is kind of random, but they are not really random numbers. By setting a seed value with this method, you can ensure that the sequence of random numbers generated by the random module will be the same every time you run your program.
The random seed is a numerical value that repeats pseudo-random numbers in Python. The value in the random seed saves the state of randomness. For example, If we call the seed function with seed value ‘1’ multiple times, the computer generates the same random numbers. When the value is not mentioned in the random seed, then the computer will use the current system time in seconds or milliseconds as a seed value to generate a different set of random numbers.
1. Quick Examples of random.seed() Function
If you are in a hurry, below are some quick examples of random.seed() function.
# Quick examples of random.seed() function
# Example 1: Initialize the seed() with 3 & get the random numbers
random.seed(3)
print(random.random())
# Example 2: Initialize the seed() with 0 & get the random numbers
random.seed(0)
print(random.random())
# Example 3: Initialize the seed() with None & get the random numbers
random.seed()
print(random.random())
# Example 4: Initialize the seed() with 5 & get the random integers
random.seed(5)
print(random.randint(10, 20))
2. Random seed() Function
The Python seed()
function is used to initialize the random number generator by providing a seed value that repeats the pseudo-random numbers. If we use the same seed value multiple times to initialize the random number generator, we get the same random numbers.
2.1 Syntax of random.seed()
Following is the syntax of the random.seed().
# Syntax of random.seed()
random.seed(a, version)
2.2 Parameters of random.seed()
It takes two parameters.
a:
(Optional) Is a seed value is used to provide seed value to a random number generator. The default value is None. If it is None random number generator uses the current system time. If it is an integer we can use it directly and if not it needs to be converted into an integer.version:
An integer is used to specify how to converta
in a integer. Default value is 2.
2.3 Return Value
It returns nothing, it just specifies the seed value.
3. Usage of random.seed() Function
Using the Python random.seed() function we can provide seed value to customize the random number generator, by default it uses the current system time.
In the below example, first, we have initialized the seed() function with a seed value(4) and then call the random() function to get the random value.
# Import random module
import random
# Initialize the seed() with 3 & get the random numbers
random.seed(3)
print("Random number of seed value 3:")
print(random.random())
print(random.random())
Yields below output.
When you run the above code another time you get the same set of random numbers as the above.
Now, change the seed value and run the same example again. Notice that the generated random values changed.
# Import random module
import random
# Initialize the seed() with 0 & get the random numbers
random.seed(0)
print("Random number of seed value 0:")
print(random.random())
print(random.random())
Yields below output.
When you run the above code another time you get the same set of random numbers as the above.
4. Set seed value None & Get the Different Random Numbers
We can get different random numbers for each run by calling the seed() function with out any argument. By executing multiple times, we get the different random numbers.
# Import random module
import random
# Initialize the seed() with None & get the random numbers
random.seed()
print("Random number of seed value None:")
print(random.random())
print(random.random())
print(random.random())
# Output:
# Random number of seed value None:
# 0.08325080335264412
# 0.2902867023198895
# 0.0310261895700632
When you run the above code another time you get a different set of random numbers than the above.
5. Use Python seed() function with Random.randint()
Use the random.seed()
function with random.randint() function and get the repeated random numbers for multiple executions. randint() function is another function of the random module in Python which is used to generate the random number between the specified range of integers.
Let’s set the seed() function with the seed value and call the randint() to get the same random integer of the specified range.
# Import random module
import random
# Initialize the seed() with 5 & get the random integers
random.seed(5)
print("Random integer of seed value 5:")
print(random.randint(10, 20))
print(random.randint(10, 20))
# Output:
# Random integer of seed value 5:
# 19
# 14
When you run the above code another time you get the same set of random numbers as the above.
6. Conclusion
In this article, I have explained Python random.seed()
function syntax, parameters, and usage of how to initialize the random number generator with examples. By setting a seed value with this method, you can ensure that the sequence of random numbers generated by the random
module will be the same every time you run your program.
Happy learning!!
Related Articles
- Random String Generation with Letters and Digits in Python
- Python random.randint() with Examples
- Python Random uniform() Function
- Python Random Module Methods Explained
- Generate Random Integers Between 0 and 9 in Python
- Python random.sample() With Examples
- Shuffle List in Python with Examples
- Python Random randrange() Function
- Generate Random Float Numbers in Python
- Python random.choice() function
- Python random.random() function
- Python random.choices() function