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

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

PHP中文网
Release: 2016-05-25 17:09:29
Original
1380 people have browsed it

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);
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