Sunday, 8 September 2013

filling numpy array with random element from another array

filling numpy array with random element from another array

Hi I'm not sure if this is possible but here goes. Suppose I have an array
as such: array1 = [0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]
and now I would like to create a numpy 1D array consisting of 5 elements
that is randomely drawn from array1 AND with the condition that the sum is
equal to 1. Example is something like, a numpy array that looks like
[.2,.2,.2,.1,.1].
currently I use the random module, and choice function that looks like
this: range1=
np.array([choice(array1),choice(array1),choice(array1),choice(array1),choice(array1)])
then checking range1 to see if it meets the criteria; I'm wondering if
there is faster way , something similiar to randomArray =
np.random.random() instead.
Would be even better if I can store this array in some library so that if
I try to generate 100 of such array, that there is no repeat but this is
not necessary.

No comments:

Post a Comment