pythonnumpyrandomrandom-seed

Size of seeds for numpy.random


I want to run some code using numpy.random and keep track of what the seed is so that if the output is interesting, I can recreate and play around with that randomly generated instance. Therefore, I want the setup to involve something like

import numpy as np
s = np.random.randint(10000000000)
print(s)
np.random.seed(s)
### remainder of code

so that the code is still running randomly, but I also have retained the seed s. The value 10000000000 was chosen arbitrarily; what is the appropriate scale for numpy.random's seeding? e.g. are seeds all the same modulo 2^32?


Solution

  • Personally, I'd use the time as a seed or SHA-256 as it's a standard rather than just a 32-bit int, here is an example of how to use the time, depending on your usecase.

    import numpy as np
    import time
    
    # Use current time as seed value
    s = int(time.time())
    print("Seed value:", s)
    
    np.random.seed(s)
    # Generate random numbers