Posted By

kendsnyder on 01/25/11


Tagged

random probability


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


Do an action 1 out of every N page requests


 / Published in: PHP
 

  1. function probabilityMatch($inNumberOfEvents) {
  2. $max = min($inNumberOfEvents, mt_getrandmax());
  3. if ($max > 0) {
  4. return (mt_rand(0, $max) == 1);
  5. }
  6. }
  7.  
  8. // Example usage
  9. // if (probabilityMatch(10)) {
  10. // clean the cache 1 out of every 10 page requests
  11. // cleanCache();
  12. // }

Report this snippet  

You need to login to post a comment.