Home >php教程 >PHP源码 >实现“随机抽奖”功能的函数

实现“随机抽奖”功能的函数

PHP中文网
PHP中文网Original
2016-05-25 17:09:291397browse

php代码

<?php
/**
 * “抽奖”函数
 *
 * @param integer $first	起始编号
 * @param integer $last		结束编号
 * @param integer $total	获奖人数
 *
 * @return string
 *
*/
function isWinner($first, $last, $total)
{
	$winner = array();
	for ($i=0;;$i++)
	{
		$number = mt_rand($first, $last);
		if (!in_array($number, $winner))
			$winner[] = $number;	// 如果数组中没有该数,将其加入到数组
		if (count($winner) == $total)	break;
	}
	return implode(&#39; &#39;, $winner);
}
// for test
echo isWinner(1, 100, 30);
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
Previous article:PHP数组操作函数Next article:参数过滤类