Choosing randomly all the elements in the the list just once

Posted by Dalek on Stack Overflow See other posts from Stack Overflow or by Dalek
Published on 2014-06-01T15:08:33Z Indexed on 2014/06/01 15:25 UTC
Read the original article Hit count: 137

Filed under:
|
|
|

How is it possible to randomly choose a number from a list with n elements, n time without picking the same element of the list twice. I wrote a code to choose the sequence number of the elements in the list but it is slow:

>>>redshift=np.array([0.92,0.17,0.51,1.33,....,0.41,0.82])
>>>redshift.shape
(1225,)
exclude=[]
k=0
ng=1225
while (k < ng):
      flag1=0
      sq=random.randint(0, ng)
      while (flag1<1):
        if sq in exclude:
           flag1=1
           sq=random.randint(0, ng)
        else:
           print sq
           exclude.append(sq)
           flag1=0
      z=redshift[sq]
      k+=1

It doesn't choose all the sequence number of elements in the list.

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy