Reputation: 45
I'm trying to generate potentially large arrays of float (0.0-1.0), but have each element driven by a seed array, comprised of integers, which represent an id.
For example, I if have these two arrays:
seedArray1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
seedArray2 = np.array([0, 1, 2, 3, 4, 5])
I'd like to be able to use those arrays as seed, but where an element is the same in both arrays (regardless of the shape of the length of the array, or where that id happens to occur in the array), it should generate the same float, like this:
[ 0.5488135 0.417022 0.4359949 0.5507979 0.96702984 0.22199317 0.89286015 0.07630829 0.8734294 0.01037415]
[ 0.5488135 0.417022 0.4359949 0.5507979 0.96702984 0.22199317]
However, when I try to set the random seed of numpy.random, I get different results:
import numpy as np
x1 = np.arange(10)
x2 = np.arange(6)
np.random.seed(x1)
print np.random.rand(x1.shape[0])
np.random.seed(x2)
print np.random.rand(x2.shape[0])
Which prints:
[ 0.41060638 0.23192123 0.91382737 0.02916066 0.91437242 0.51045425 0.26140116 0.16574922 0.08861477 0.31908955]
[ 0.52500897 0.98236732 0.99371835 0.14121932 0.66945133 0.24223304]
Is there a way to generate random numpy arrays, using each element as the seed, and still leverages numpy's speed?
Upvotes: 3
Views: 406
Reputation: 13430
Not really, no. PRNGs are designed to output streams of many numbers from a single seed, and numpy.random
's API is designed around that. You can of course do the loop yourself if you need an arbitrary, repeatable mapping from integers to floats within [0, 1)
for some reason. There might be other such mappings that would be easier and faster to use.
y = [np.random.RandomState(seed).rand() for seed in seed_array]
Upvotes: 4