I want to seed numpy.random.RandomState instance with an hashlib generated hash to have pseudorandom source always generating same values for same input data. When I try to do that this way:
hash = sha256(some_data)
RandomState(seed=hash.digest())
I get:
ValueError: object of too small depth for desired array
I could do map(ord, hash.digest())
but I don't know if it is right solution. Should I do that or will I lose randomness?
First of all, the random state seed is a 32-bit unsigned integer, so it will not be able to use the full 256-bit hash you are generating.
Thanks to the commenter below for pointing out that the seed can be an array of 32-bit (unsigned) integers; you can convert the hash to such an array as follows:
import numpy as np
from hashlib import sha256
data = np.random.rand(1000)
hash = sha256(data)
seed = np.frombuffer(hash.digest(), dtype='uint32')
rstate = np.random.RandomState(seed)
If 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