<?php
/**
* rand qq
*/
if(file_exists('qq.txt'))
$qq_max = file_get_contents('qq.txt');
else
$qq_max = 10000000;
/*check qq*/
function check_qq($qq_max){
if(check_num($qq_max) && continuous($qq_max))
return true;
else
return false;
}
/*检查不同数字的数量,数量小于长度一半的不返回*/
function check_num($qq_max){
$a = array();
$qq_max .= '';
for($i=0; $i<strlen($qq_max); $i++){
if(!in_array($qq_max[$i], $a))
$a[] = $qq_max[$i];
}
if(count($a) > strlen($qq_max)/2)
return true;
else
return false;
}
/*检查号码连续,小于一半连续的*/
function continuous($qq_max){
$a = 0;
$b = array();
$c = 0;//最大连续次数
$qq_max .= '';
for($i=0; $i<strlen($qq_max); $i++){
if($i==0){
$b[$a] = 1;
$c = 1;
continue;
}
if($qq_max[$i] - $qq_max[$i-1] == 1){
if(isset($b[$a])){
$b[$a]++;
$c = $c < $b[$a] ? $b[$a] : $c;
}else
$b[$a] = 1;
}else
$a++;
}
if($c < strlen($qq_max)/2)
return true;
else
return false;
}
function rand_qq($qq_max){
$qq_max++;
if(!check_qq($qq_max)){
$qq_max = rand_qq($qq_max);
}
return $qq_max;
}
$qq = rand_qq($qq_max);
echo $qq;
file_put_contents('qq.txt', $qq);
转载请注明:小Y » 有一个需求,要求生成全局唯一号码(类似QQ号),但是要把好的号码预先留下或者在用户注册的时候判断将要获取的号码是不是好号码
php实现时间轮算法