title: Generate random number with given probability
body: <p>I have a question which is basically the vectorized R solution to the following matlab problem:</p>
  
  <p><a href="https://stackoverflow.com/questions/13914066/generate-random-number-with-given-probability-matlab">Generate random number with given probability matlab</a></p>
  
  <p>I'm able to generate the random event outcome based on random uniform number and the given probability for each single event (summing to 100% - only one event may happen) by:</p>
  
  <pre><code>sum(runif(1,0,1) &gt;= cumsum(wdOff))
  </code></pre>
  
  <p>However the function only takes a single random uniform number, whereas I want it to take a vector of random uniform numbers and output the corresponding events for these entries.</p>
  
  <p>So basically I'm looking for the R solution to Oleg's vectorized solution in matlab (from the comments to the matlab solution):</p>
  
  <p>"Vectorized solution: <code>sum(bsxfun(@ge, r, cumsum([0, prob]),2)</code> where <code>r</code> is a column vector and <code>prob</code> a row vector. –  Oleg"</p>
  
  <p>Tell me if you need more information.</p>