title: php percentage chance
body: <p>This is really more a question of approach, but I'm presenting it in php.</p>
  
  <p>Suppose we had a list of four percentages that a give event will occur on iteration.</p>
  
  <pre><code>array=('walk the dog'=&gt;.25,'read the paper'=&gt;.25,'drink coffee'=&gt;.0,'listen to music'=&gt;.50)
  </code></pre>
  
  <p>(Keys are just tests - in practice this is going to be used in a strategy pattern that applies different methods to an object off chance using <code>call_user_func()</code>)</p>
  
  <p>In a loop, what would be the best way to choose one of these events by random-adjusted selection taking their respective weights into account (IE, for this particular configuration, there will be a lot of music listened to and not a lot of coffee drank)</p>
  
  <p>Currently I have something like:</p>
  
  <pre><code>for($i=1;$i&lt;=3;$i++){
    $rand = 1/rand(1,10);
    //choose from the list
  }
  </code></pre>
  
  <p>But this would require mapping the percentages to value ranges (50-100% become listening to music, 0-25% walking the dog and 25-50 reading the paper) because the 'value' for that iteration is calculated as a floating point number between 0 and 1. This kind of changes the way the percentages need to be recorded.</p>
  
  <p>Is there any kind of approach that would preserve the identity of the values as the real probability they should be chosen rather than ranges?  It would definitely make the code more configurable since I have to make these values adjustable.  Please let me know if I've been too vague.</p>