title: Random index of a non zero value in a numpy array
body: <p>I hope this hasn't been asked before. Not sure how I can reformulate the question. This <a href="https://stackoverflow.com/questions/27414908/how-to-randomly-select-some-non-zero-elements-from-a-numpy-ndarray">post</a> is quite similar but still not exactly what I'm looking for.</p>
  
  <p>I have a numpy array with a variable length (something between 4-12). The values of the array are either 0 or 1. I'd like to get the index of a random sample from the array, which is not 0. </p>
  
  <p>My idea was to do something like this:</p>
  
  <pre><code>def sample(self):
          flag = 0
          while flag == 0:
              sampleValue = randint(0, len(myArray())-1)
              flag = myArray()[sampleValue]
          return sampleValue
  </code></pre>
  
  <p>But that's not really good code and will eventually get trapped in a never ending while loop. Of course I could improve this. Chances are high there's a much more beautiful way of doing this in python :)</p>