1. 程式人生 > >PHP 隨機生成自定義位數隨機數

PHP 隨機生成自定義位數隨機數

	
	function randStr($len=2,$format='ALL') { 
 switch($format) { 
 case 'ALL':
 $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break;
 case 'CHAR':
 $chars='[email protected]#~'; break;
 case 'NUMBER':
 $chars='0123456789'; break;
 default :
 $chars='[email protected]#~'; 
 break;
 }
 mt_srand((double)microtime()*1000000*getmypid()); 
 $password="";
 while(strlen($password)<$len)
    $password.=substr($chars,(mt_rand()%strlen($chars)),1);
 return $password;
 }