PHP之生成不重复随机字符串

function getOptions()
{
 $options = array();
 $result=array();
 for($i=48;$i<=57;$i++)
 {
     array_push($options,chr($i)); 
 }
 for($i=65;$i<=90;$i++)
 {
      $j =32;
      $small =$i+$j;
      array_push($options,chr($small));
     
 }
 return $options;

}

$len=10;
for($j=0;$j<1000000;$j++)
{
 $result="";
 $options =getOptions();
 $lastIndex=35;
 while (strlen($result)<$len)
 {
  $index = rand(0,$lastIndex);
  $chr = $options[$index];
  $result .=$chr;
  $lastIndex = $lastIndex-1;
  $options[$index]=$options[$lastIndex];
 }
 echo $result."\n";
}

 

 

Enjoy

 

posted @ 2010-05-19 17:23  bigdog  阅读(1336)  评论(0编辑  收藏  举报