Use Python
random
library to initialize the pseudo-random number generator.
Reference
» Python random.seed() to initialize the pseudo-random number generator
» Python Random Module to Generate random Numbers and Data
1. Goals of This Article is to answer:
- What
random.seed()
does in Python and how to use it. - How to choose the right seed value to generate the random data that you want.
- How to use a
random.seed()
function with other random module functions.
2. Basic Points:
-
Random number or data generatod by Python’s random module is not truly random, it’s pseudo-random (it’s PRNG), i.e. deterministic. It generate the numbers on the basis of some value. This value is nothing but a seed value. In short, numbers generated by the random module depend on the seed value.
-
Generally, the seed value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value, so by default, current system time is used as the seed value.
-
Using a custom seed value we can initialize the pseudo-random number generator the way we want.
3. random.seed()
3.1. Syntax of random.seed()
:
random.seed(a=None, version=2)
The function accepts two arguments. Both are the optional arguments.
a
is the seed value. If the a value isNone
, then by default current system time is used. if seed value is in the form of integer it is used as it is.version=2
is the default version. In this version, a string, bytes, or byte-array object gets converted to an int.
3.2. Import points on the seed value:
-
The random module uses the seed value as a base to generate a random number. if seed value is not present it takes the system’s current time.
- If you don’t initialize the pseudorandom number generator using a
random.seed()
, internally it will automatically call therandom.seed()
and assign system current time to the seed value. - That’s why whenever we execute random.random() we always get a different value.
- The seed value is very important in the field of computer security to pseudorandomly generate a strong secret encryption key.
3.3. Example:
import random
#if you want to use the same random number once again, then use the same seed value
print ("Random number with seed 30")
random.seed( 30 )
print ("first - ", random.randint(25,50)) #will generate a random number
#will generate a same random number as previous
random.seed( 30 )
print ("Second - ", random.randint(25,50))
#will generate a same random number as previous
random.seed( 30 )
print ("Third - ", random.randint(25,50))
Output:
Random number with seed 30
first - 42
Second - 42
Third - 42
4. random.seed
Using Combos
4.1. with choice()
method together
The random.choice()
method is used to choose a random element from the list and set. You can generate the same choice every time by using the same seed value right before the choice method:
list = [100,200,300,400,500,600]
random.seed(6)
random_item = random.choice(list)
print ("First random item from list ", random_item)
random.seed(6)
random_item = random.choice(list)
print ("Second random item from list ", random_item)
random.seed(6)
random_item = random.choice(list)
print ("Third random item from list ", random_item)
Output:
First random item from list 500
Second random item from list 500
Third random item from list 500
4.2. with shuffle()
method together
The main purpose of using seed
and shuffle
method together is to produce the same result every time after each shuffle.
If we set the same seed value every time before calling the shuffle method we will get same item sequence.
import random
numbers = [10, 20, 30, 40, 50, 60]
print ("Original list: ", numbers )
random.seed(4)
random.shuffle(numbers)
print("reshuffled list ", numbers)
numbers = [10, 20, 30, 40, 50, 60]
random.seed(4)
random.shuffle(numbers)
print("reshuffled list ", numbers)
Output:
Original list: [10, 20, 30, 40, 50, 60]
reshuffled list [40, 60, 50, 10, 30, 20]
reshuffled list [40, 60, 50, 10, 30, 20]
4.3. with sample()
method together
Using sample method we can generate multiple random items from the list and other sequence types.
If you want to generate the same random items out of the list every time then set the same seed value before calling a sample method.
import random
#using random.seed() and random.sample() together
fruit_list = ["Apple", "Mango", "Banana", "Apricot", "Cherries", "Grape", "Kiwi"]
random.seed(3)
sample_list = random.sample(fruit_list, 3)
print("First sample fruit list ", sample_list)
random.seed(3)
sample_list = random.sample(fruit_list, 3)
print("Second sample fruit list ", sample_list)
random.seed(3)
sample_list = random.sample(fruit_list, 3)
print("Third sample fruit list ", sample_list)
Output:
First sample fruit list ['Mango', 'Cherries', 'Grape']
Second sample fruit list ['Mango', 'Cherries', 'Grape']
Third sample fruit list ['Mango', 'Cherries', 'Grape']
KF