which one is better?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

which one is better?

Post 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
realnsleo
Forum Newbie
Posts: 15
Joined: Sat May 16, 2009 12:08 pm

Re: which one is better?

Post 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;
   }
Post Reply