title: Select a candidate based on count and percentage in PHP
body: <p>I have a list of ads, I would like to rotate them based on the rotation (which represents a percentage) and the number of times it has been viewed. There can be any number of ads, and they are listed within a multidimensional array. Each line is one ad. Each time it is viewed, viewed increases +1.</p>
  
  <pre><code>$aAd[] = array('viewed' =&gt; 2003, 'rotation' =&gt; 70); // 70%
  $aAd[] = array('viewed' =&gt; 122, 'rotation' =&gt; 30); // 30%
  </code></pre>
  
  <p>I am trying to work out how to select the next ad to display based on the rotation and the number of times it has been viewed. Is anyone able to help with the formula to do this? </p>
  
  <p>The algorithm needs to select the next one to display, it should choose it based on the number of times it has been viewed and the percentage it should be viewed (the rotation). The order of the ads are not presently in any specific order. The rotation represents the desired percentage in which the client would like the ads displayed. It is fine if an ad is picked twice in a row, as long as the rotation equals out to 70/30 in this example. Keep in mind, there can be any number of ads and they can have different rotations assigned as well.</p>