jaboja
jaboja

Reputation: 2237

Seed numpy.random.RandomState with hashlib hash

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?

Upvotes: 4

Views: 1390

Answers (1)

jakevdp
jakevdp

Reputation: 86443

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)

Upvotes: 5

Related Questions