title: Select element from array with probability proportional to its value
body: <p>I have an array of doubles and I want to select a value from it with the probability of each value being selected being inversely proportional to its value. For example:</p>
  
  <pre><code>arr[0] = 100
  arr[1] = 200
  </code></pre>
  
  <p>In this example, element 0 would have a 66% of being selected and element 1 a 33% chance. I am having difficulty coding this. What I have done so far is to calculate the total value of the array (the example would be 300), then I've played around with inversing the numbers before calculating them as a percentage of the total. I can't get anything to work. In the end I desire:</p>
  
  <pre><code>new randomNumber
  for(int y=0; y &lt; probabilities.length; y++){
       if(randomNumber &lt; probabilities[y]){
            Select probabilities[y]
       }
  }
  </code></pre>
  
  <p>Or something to that affect. Any help? Coding is in Java but I can adapt any pseudocode.</p>