基于时间的中奖几率算法
程序员文章站
2022-03-24 23:05:23
...
php代码
function getProb($startTime,$endTime) { $curTime = time(); if($curTime>$endTime || $curTimeget($memKey); $redis->setex($memKey,3600*24*20,$curTime); if(empty($prevTime) || $prevTime<$startTime) $prevTime = $startTime; $prob = round(($curTime - $prevTime)/($endTime - $prevTime),3); return $prob; } $num = 8;//剩余库存 $startTime = strtotime(date('Y-m-d 00:00:00')); $endTime = strtotime(date('Y-m-d 23:59:59')); $prob = getProb($startTime,$endTime); $prob *= $num; if(rand(0,1000)/1000<=$prob) { echo 'ok'; }