With the random
module, you are able to seed it to get the same values every time.
import random
random.seed(1)
print(random.randint(1,100)) # outputs 18 every time
lst = [1,2,3]
random.shuffle(lst)
print(lst) # [2, 3, 1] every time
Is there a CSPRNG that can do this? For example, according to this question How can I create a random number that is cryptographically secure in python?, random.SystemRandom
is secure. But seeding it doesn't return the same thing.
from random import SystemRandom
s = SystemRandom()
s.seed(1)
print(s.randint(1,100)) # 81, 16, 100, 58
lst = [1,2,3]
s.shuffle(lst)
print(lst) # [1, 3, 2], [3, 2, 1]
Does such a CSPRNG exist? or does this negate the security aspect?
The randomgen
package provides NumPy compatible CSPRNGs, e.g. ChaCha which can be used as:
import numpy as np
from randomgen import ChaCha
rg = np.random.Generator(ChaCha(seed=1234, rounds=8))
rg.integers(1, 100)
Notes:
randomgen
provides its own Generator
, but it's deprecated and moving into numpyIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With