I would like to choose a random seed for numpy.random
and save it to a variable. I can set the seed using numpy.random.seed(seed=None
) but how do you
You can't… but there's really no good reason to do so. Unless you're actually trying to reproduce the behavior of seed
, rather than put the RNG into a repeatable state, you're trying to add an extra level of indirection for no reason.
If you want to stash and restore the RandomState, do that, using the get_state()
and set_state()
functions.
If you really want to use seed
instead, you can just use np.random
to generate a random seed (e.g., via random_integers(0, 255, SOME_LENGTH)
), which you can stash and reuse later. But there's not much reason to do that.
Or, of course, you can call Python's os.urandom to create a seed the same way NumPy does by default. Note that the docs explicitly say that:
If
seed
isNone
, thenRandomState
will try to "read date from/dev/urandom
(or the Windows analogue) if available or seed from the clock otherwise.
But again, there's not much reason to do that either. (Also, it isn't documented how much randomness it gets from urandom
, so there's always the risk that you'll be seeding it with less random data than it normally uses, or wastefully gathering too much.)