Home  >  Article  >  php教程  >  基于时间的中奖几率算法

基于时间的中奖几率算法

PHP中文网
PHP中文网Original
2016-05-25 16:58:091367browse

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(&#39;Y-m-d 00:00:00&#39;));
$endTime = strtotime(date(&#39;Y-m-d 23:59:59&#39;));

$prob = getProb($startTime,$endTime);

$prob *= $num;

if(rand(0,1000)/1000<=$prob)
{
	echo &#39;ok&#39;;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn