title: Extract values from a set according to a given probability distribution
body: <p>I have to tackle this problem:</p>
  
  <pre><code>vector&lt;int&gt; myset={1,2,3,4};
  vector&lt;double&gt; distribution ={0.01,0.1,0.3,0.59};
  </code></pre>
  
  <p>I have to pick a number of values from <em>myset</em> according to given <em>distribution</em>. Actually <em>distribution</em> and <em>myset</em> aren't fixed. They are of the same dimension, but this dimension can change and be  pretty large too. I could uniformly extract a number in the range [0 100] and do like this:</p>
  
  <pre><code>int extracted_numb;
  int numb = rand(0,100);
  if(numb&lt;1)
    extracted_numb=myset[0];
  else if(numb&lt;11)
    extracted_numb=myset[1];
  else if(numb&lt;41)
    extracted_numb=myset[2];
  else
    extracted_numb=myset[3];
  </code></pre>
  
  <p>But I repeat, I don't know in the real case the dimension of <em>distribution</em> and <em>myset</em> (because is a user parameter) and thus I don't know how many <em>if</em>  to do.</p>
  
  <p>I ask if there is a good algorithm for this problem, and maybe some native library of C++ or in Boost that already accomplish it</p>
  
  <p>(I'm using Boost 1.63 and C++11)</p>