title: Random index with custom probability distribution
body: <p>I have a vector of probabilities (which of course sum 1):</p>
  
  <pre><code>prob = [0.1, 0.3, 0.4, 0.2]
  </code></pre>
  
  <p>Now I need to generate a random index for this vector (a number between 0 and 3 both included) but I want that the probability of each index is given by prob</p>
  
  <pre><code>0 will be generated with prob 0.1
  1 will be generated with prob 0.3
  2 will be generated with prob 0.4
  3 will be generated with prob 0.2
  </code></pre>
  
  <p>I know that I can do this by calculating the cumsum</p>
  
  <pre><code>cumsum = [0.1, 0.4, 0.8, 1.0]
  </code></pre>
  
  <p>Then generating a random number between 0 and 1:</p>
  
  <pre><code>rand_num = np.random.random()
  </code></pre>
  
  <p>And finally use <code>np.digitize</code> to check in which bin my random number falls.</p>
  
  <pre><code>idx = np.digitize([rand_num], cumsum)
  </code></pre>
  
  <p>This works and I'm happy with this, digitize even accepts a list of numbers and classifies them into the bins, so I can create my own function to generate indexes given a probability distribution.</p>
  
  <p>My question is: This is a common problem, so doesn't a function already exist that does this? (And that will be more efficient than doing it myself)</p>
  
  <p>Thanks</p>