随机生成特殊6位数的算法如何写

随机生成不重复的6位数,有以下几点要求:

      1、不能出现8这个数字
      2、不能出现升序和降序 例如 123456 987654等,局部也不能出现3位以上的,比如123590、654923等
      3、不能出现连续2位及以上相同的数字,例如112233。

 

需求分析:

      1、不能出现8这个数字
      2、不能出现升序和降序 例如 123456 987654等,局部也不能出现3位以上的,比如123590、654923等 [951374也应该不合要求,因为951是↓ 137是↑]

function grand($len = 6) 
{
  $source = '012345679';
  $first = rand(0,8);
  $code=$source[$first];
  $step=$first==8?0:($first==0?1:rand(0,1));
  for($i=1;$i<$len;$i++)
  {
    if($step)
    {
       $step=0;   
       $next_step=($first+1)>8?8:($first+1);
       $next=$source[rand($next_step,8)];
    }
    else
    {
       $step=1;
       $next_step=($first-1)<0?0:($first-1);
       $next=$source[rand(0,$next_step)];
    }
    $code.=$next;
  }
  return $code;
}

 

300*300
 文章首页关于迷茫时代关于我写意人生
版权所有:迷茫时代 All rights reserved   
执行时间:0.00445 秒