Home >Backend Development >PHP Tutorial > 新人 怎么生成不重复的4位数字

新人 怎么生成不重复的4位数字

WBOY
WBOYOriginal
2016-06-13 13:18:431001browse

新人求助 如何生成不重复的4位数字
//随即生成四位数
function sc(){
  for($i=0;$i $ygsz=rand(0,9);
$sgesz.=$ygsz;
}
  for($a=0;$a if($sgesz[$a]==$sgesz[$a+1]){
$vv=sc();
}



  }
  return $sgesz;
}
$zqsz=sc();





?>
怎么改呢

------解决方案--------------------

PHP code
function sc() {
    $ar = array();
    for ($i = 0; $i <font color="#e78608">------解决方案--------------------</font><br>
PHP code

 $arr = Array('0','1','2','3','4','5','6','7','8','9');//定义数组
 shuffle($arr);//打乱元素顺序
 $rand = array_slice($arr,0,4);//取前四个元素

 $result=implode('',$rand);//转成字符串
 echo $result; <div class="clear">
                 
              
              
        
            </div>
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