vendredi 20 mai 2016

Is there a predictable replacement for os.urandom using Python's random module?

I'm looking to get N random bytes as a bytearray or bytes, as os.urandom does.

However I need to be able to set a random seed so the value is reproducable.

Is there a more direct way to do this besides creating many ints and converting to bytes?

def urandom_from_random(rng, length):
    return bytes([rng.randint(0, 255) for i in range(length)])

import random
rng = random.Random(42)
data = urandom_from_random(rng, 120)




Aucun commentaire:

Enregistrer un commentaire