1

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
6
  • 1
    Possible duplicate of Random hash in Python Commented Oct 19, 2019 at 22:34
  • 2
    What do you mean by "seed"? It looks like what you want to ask is not what you are asking. Commented Oct 19, 2019 at 22:50
  • @PedroRodrigues while the question is poorly formulated, to my understanding it seems he wanted both random integers and strings so not really a duplicate of that question. Commented Oct 19, 2019 at 23:39
  • @RMPR there are a ton of different hashes in the answers to that question. This one, for example, looks pretty similar to the expected output the OP asked for. I guess you meant random ints and strings in the output. I am missing something? Commented Oct 19, 2019 at 23:42
  • 1
    OP, in case you aren't aware, "seed" usually refers to the input of an RNG. For some RNG implementations, it is possible to extract the seed from an existing RNG. Python's default RNG is a mersenne twister, which has some awkward problems if you try to use a human-sized seed. Other RNGs like PCG are better in a lot of ways (as long as you aren't doing crypto). Commented Oct 19, 2019 at 23:50

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

An additional processing is required or am I missing something ?
@RMPR Of course it is up to the user how to interpret those bytes (as numbers, chars, ...).
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.