如何在PHP中编写函数来调用给定函数(匿名)X%
?
例如:
function call_weighted(0.1, function() {
echo 'this is actually run approximately 10% of the times it is called';
});
答案 0 :(得分:2)
function call_weighted($weight, $function) {
if (mt_rand() < $weight * mt_getrandmax()) {
$function();
}
}