public static function getFixRand($weight) {
if ((int)$weight < 0 || (int)$weight > 100) {
return -1;
}
$weightArr = [
1 => $weight,
2 => 100-$weight,
];
$result = '';
//概率数组的总概率精度
$sumWeight = array_sum($weightArr); //计算数组中元素的和
$luckyNum = mt_rand(1, $sumWeight);
//概率数组循环
$beginCursor = $endCursor = 0;
foreach ($weightArr as $id => $w) {
$beginCursor = $endCursor;
$endCursor += $w;
if ($luckyNum > $beginCursor && $luckyNum <= $endCursor) {
$result = $id;
break;
}
}
if ($result == 1) {
return true;
} else {
return false;
}
}
PHP获取固定概率
原创
©著作权归作者所有:来自51CTO博客作者TBHacker的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:PHP概率,抽奖
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
php获取外部环境变量的几种方式
php环境变量的使用
环境变量 php windows操作系统 -
php抽奖概率算法
php抽奖概率算法
算法php -
php抽奖概率算法(刮刮卡,大转盘)
实例为大家分享了php中奖概率
数组 php 概率算法