实际上在PHP中运行一定比例的函数

时间:2014-05-30 23:08:44

标签: php random anonymous-function

如何在PHP中编写函数来调用给定函数(匿名)X%

例如:

function call_weighted(0.1, function() {
      echo 'this is actually run approximately 10% of the times it is called';
});

1 个答案:

答案 0 :(得分:2)

function call_weighted($weight, $function) {
    if (mt_rand() < $weight * mt_getrandmax()) {
        $function();
    }
}