Page 1 of 1

which one is better?

Posted: Wed Mar 10, 2010 10:43 pm
by PHPycho
Which one do you prefer among the two?

Code: Select all

function genRandomNo($length = 5){  
    $string = substr(md5(uniqid(rand(), true)), 0, $length);
    return $string;
}
Vs

Code: Select all

function genRandomNo($length = 5){  
    $string = substr(md5(microtime() * mktime()), 0, $length);
    return $string;
}
also you can suggest alternatives.

Thanks

Re: which one is better?

Posted: Thu Mar 11, 2010 12:03 am
by realnsleo
i like first one. also try:

Code: Select all

function generateRandStr($length){
      $randstr = "";
      for($i=0; $i<$length; $i++){
         $randnum = mt_rand(0,61);
         if($randnum < 10){
            $randstr .= chr($randnum+48);
         }else if($randnum < 36){
            $randstr .= chr($randnum+55);
         }else{
            $randstr .= chr($randnum+61);
         }
      }
      return $randstr;
   }