Agostino - 17 days ago 4x

Python Question

I want to generate a random **seed** in a predictable way.

I was hoping to do this

`seed = 12345`

prng_0 = random.Random(seed)

prng_1 = random.Random(prng_0.rand_int(0))

There,

`0`

If you are curious about my reason, I'm doing this because I need reproducibility when testing. Namely, this is a function receiving a seed and building its prng,

`prng_0`

`def funct_a(seed=None):`

prng_1 = random.Random(seed)

prng_2 = numpy.random.RandomState(prng_1.randint(0, 4294967296))

print(prng_1.random())

print(prng_2.random())

def funct_b(seed=None):

prng_0 = random.Random(seed)

for i in range(0, 5):

seed = prng_0.randint(0) # not working, needs upper bound

funct_a(seed)

funct_b(12345) # test call

EDIT: interestingly enough, numpy (which I'm also using) has a definite upper seed value, as testified by the doc and by this error

ValueError: Seed must be between 0 and 4294967295

Answer

When I don't want an upper bound I'll often use `sys.maxint`

for the upper bound as an approximation

Source (Stackoverflow)

Comments