Home > php教程 > PHP源码 > body text

基于时间的中奖几率算法

PHP中文网
Release: 2016-05-25 16:58:09
Original
1369 people have browsed it

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;;
}
Copy after login

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template