商品活动抽奖概率算法
商品抽奖概率算法。可用于随机抽取一定概率的商品。
public function rand_rate($data)
{
$roll = rand(1, array_sum($data));
$tmp = 0;
$rollnum = 0;
foreach ($data as $k => $v) {
$min = $tmp;
$tmp += $v;
$max = $tmp;
if ($roll > $min && $roll <= $max) {
$rollnum = $k;
break;
}
}
return $rollnum;
}