Generate multiple independent random streams in python

For the sake of reproducibility you can pass a seed directly to random.Random() and then call variables from there. Each initiated instance would then run independently from the other. For example, if you run:

import random
rg1 = random.Random(1)
rg2 = random.Random(2)
rg3 = random.Random(1)
for i in range(5): print(rg1.random())
print('')
for i in range(5): print(rg2.random())
print('')
for i in range(5): print(rg3.random())

You'll get:

0.134364244112
0.847433736937
0.763774618977
0.255069025739
0.495435087092

0.956034271889
0.947827487059
0.0565513677268
0.0848719951589
0.835498878129

0.134364244112
0.847433736937
0.763774618977
0.255069025739
0.495435087092

You do not need to use the RandomGen package. Simply initiate two streams would suffice. For example:

import numpy as np
prng1 = np.random.RandomState()
prng2 = np.random.RandomState()
prng1.seed(1)
prng2.seed(1)

Now if you progress both streams using prngX.rand(), you will find that the two streams will give you identical results, which means they are independent streams with the same seed.

To use the random package, simply swap out np.random.RandomState() for random.Random().


Both Numpy and the internal random generators have instantiatable classes.

For just random:

import random
random_generator = random.Random()
random_generator.random()
#>>> 0.9493959884174072

And for Numpy:

import numpy
random_generator = numpy.random.RandomState()
random_generator.uniform(0, 1, 10)
#>>> array([ 0.98992857,  0.83503764,  0.00337241,  0.76597264,  0.61333436,
#>>>         0.0916262 ,  0.52129459,  0.44857548,  0.86692693,  0.21150068])