Python random module: why we need to set seed?

I was Googling what random.seed() does and found that the random module in Python in fact implements pseudo-random number generators.

We need to set a seed because the pseudo-random number generators work by performing some operation on a value. Different seed will generate different number sequences. In the example below, if we set the seed to be 123, we can find that the generated number sequences are same! That’s why we call it “pseudo-random”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> import random
>>> random.seed(123) # We set a seed first
>>> random.random()
0.052363598850944326
>>> random.random()
0.08718667752263232
>>> random.random()
0.4072417636703983
>>> random.seed(23) # We set a different seed
>>> random.random()
0.9248652516259452
>>> random.random()
0.9486057779931771
>>> random.seed(123) # If we set a same seed again, we will generate a same sequence!
>>> random.random()
0.052363598850944326
>>> random.random()
0.08718667752263232
>>> random.seed(23)
>>> random.random()
0.9248652516259452
>>> random.random()
0.9486057779931771

Reference:
https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do