Is there a way to generate random seed? something like this:
def random_seed(length)
output example:
3273650411015667511766
or is there a way to do both nums and letters? output example:
ryf65s4d4s6df54s6y4fs6f5b4y6s4fy
Is there a way to generate random seed? something like this:
def random_seed(length)
output example:
3273650411015667511766
or is there a way to do both nums and letters? output example:
ryf65s4d4s6df54s6y4fs6f5b4y6s4fy
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.
this seems to work;
import random
length=random.randint(1,30)
chars=[char for char in '0123456789abcdefghijklmnopqrstuvwxyz']
print(chars)
seed=''
for i in range(length): seed=seed+random.choice(chars)
print(seed)
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