Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.
Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.
Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.
Answer from Eric Finn on Stack OverflowExplain random.seed() like I’m five.
What does `random.seed()` do in Python? - Stack Overflow
Generate Random Seed In Python - Stack Overflow
Random Number Seed Generation
Videos
Hey python wizards. I’m just getting started with python, and I’m unsure what random seed does? I’ve gotten as far as it sorta provides a starting point for the algorithm to ensure a testable result, but how on earth does it compute the values next when doing e.g. random.randint()? Could someone explain it to me like I’m five?
Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.
Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.
Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.
All the other answers don't seem to explain the use of random.seed().
Here is a simple example (source):
import random
random.seed(3)
print("Random number with seed 3 :", random.random()) # will generate a random number
# if you want to use the same random number once again in your program
random.seed(3)
random.random() # same random number as before
For the integers:
import random
def random_seed(length):
random.seed()
min = 10**(length-1)
max = 9*min + (min-1)
return random.randint(min, max)
For the strings, a naive approach may be:
import random
choices = '0123456789abcdefghijklmnopqrstuvwxyz'
def random_char(pos):
return random.choice(choices)
def random_seed(length):
l = [None]*length
random.seed()
return "".join(list(map(random_char, l)))
Beware, this shouldn't be used for security purpose you must rely on os.urandom instead.
You can use os.urandom. It let's you specify the number of random bytes you want to generate. Of course you can convert / interpret these bytes as whatever you like (integers, chars, ...). For example:
>>> import os
>>> os.urandom(16)
b'-\xca(\xd2\xf7 \xe3:\x8fj\x19#\xe0-\xb8X'
os.urandom will use the OS source of randomness (such as sensor values, etc). If you call random.seed() without arguments it will also fallback on OS randomness if available, otherwise the current system time.
Possible ways to interpret the bytes:
>>> int.from_bytes(os.urandom(16), 'big')
305697826269747251034239012950993064203