title: Weighted random selection from array
body: <p>I would like to randomly select one element from an array, but each element has a known probability of selection.</p>
  
  <p>All chances together (within the array) sums to 1.</p>
  
  <p>What algorithm would you suggest as the fastest and most suitable for huge calculations?</p>
  
  <p>Example:</p>
  
  <pre><code>id =&gt; chance
  array[
      0 =&gt; 0.8
      1 =&gt; 0.2
  ]
  </code></pre>
  
  <p>for this pseudocode, the algorithm in question should on multiple calls statistically return four elements on id <code>0</code> for one element on id <code>1</code>.</p>