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
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Wed Mar 10, 2010 10:43 pm
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
Post
by realnsleo » Thu Mar 11, 2010 12:03 am
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;
}