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
RMPR
  • 3,368
  • 4
  • 19
  • 31
Oak__
  • 67
  • 7
  • 1
    Possible duplicate of [Random hash in Python](https://stackoverflow.com/questions/976577/random-hash-in-python) – Pedro Rodrigues Oct 19 '19 at 22:34
  • 2
    What do you mean by "seed"? It looks like what you want to ask is not what you are asking. – zvone Oct 19 '19 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. – RMPR Oct 19 '19 at 23:39
  • @RMPR there are a ton of different hashes in the answers to that question. [This one](https://stackoverflow.com/a/9816388/3343753), for example, looks pretty similar to the expected output the OP asked for. I guess you meant random `int`s and `string`s in the output. I am missing something? – Pedro Rodrigues Oct 19 '19 at 23:42
  • If you look my answer, I wrote two methods, the first only for random `int`s and the second for random `int`s and `string`s and because of the former I think those two questions aren't the same. – RMPR Oct 19 '19 at 23:45
  • 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). – o11c Oct 19 '19 at 23:50

3 Answers3

2

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.

RMPR
  • 3,368
  • 4
  • 19
  • 31
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)
3NiGMa
  • 545
  • 1
  • 9
  • 24
1

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
a_guest
  • 34,165
  • 12
  • 64
  • 118