在PHP中实际运行函数的时间是一定百分比的


Actually run a function a certain percentage of the time in PHP

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

例如:

function call_weighted(0.1, function() {
      echo 'this is actually run approximately 10% of the times it is called';
});
function call_weighted($weight, $function) {
    if (mt_rand() < $weight * mt_getrandmax()) {
        $function();
    }
}