/**
* 夺宝类
* Class IndianaHelper
* @package commonutil
*/
class IndianaHelper
{
/**
* 用户实例
* IndianaHelper::init(1000,10001);
* $rand_num = IndianaHelper::GetRandomNum();
* @var int
*/
public static $total = 1000;//总需人数【自定义】
public static $base_serial = 100000;//原始数,夺宝号码的开始点【可以看自己定义】
public static $rand_array = [];
public static function init($total,$base_serial){
if(!empty($total)) self::$total = $total;
if(!empty($base_serial)) self::$base_serial = $base_serial;
self::$rand_array = range(self::$base_serial, self::$base_serial + self::$total);
}
//随机获取夺宝号码
public static function GetRandomNum(){
$array = &self::$rand_array;
$length = count($array);//计算数组长度
$key = rand(0, $length-1);//获取不超过数组长度的随机数
$value = $array[$key];//获得一个随机夺宝号码
unset($array[$key]);//将该号码删除
sort($array);//数组重新排序
return $value;//返回夺宝号码
}
}
原文链接