We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Code for generating a random string

krutarth941
krutarth941 asked
on
Medium Priority
301 Views
Last Modified: 2012-08-13
Hello, can you please explain me the following code for generating a random string.

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;
Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
It generate random numbers and convert them to chars using chr() method.

chr() returns a one-character string containing the character specified by ascii:
http://php.net/manual/en/function.chr.php
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thank you for the information.

I don't understand why it adds 48, then 55 and then 61.
I don't understand this portion. Could you please explain me in brief.
$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;

Author

Commented:
Thank you very much Frozenice. I got it.

Author

Commented:
Excellent answer

Commented:
Hi

function generateRandStr($length) //Function declaration which is overriding method with Parameter.
{  
$randstr = "";   //Defining string null.
//Loop through value of parameter passed
for($i=0; $i<$length; $i++)  {  
/* mt_rand is PHP Function to generate random value. Calling that function will return random value with between 0 - 61 where 0 is min & 61 is max. More info :http://php.net/manual/en/function.mt-rand.php*/
$randnum = mt_rand(0,61);   // store mt_rand return value to $randnum
 
/*if condition to check for value fetch is less than with given value.
chr() will returns a one-character string containing the character specified by ascii.  More info http://php.net/manual/en/function.chr.php   

You can also get ascii value details here http://www.asciitable.com/
*/

if($randnum < 10){  
$randstr .= chr($randnum+48);
}else if($randnum < 36){
$randstr .= chr($randnum+55);
}else{
$randstr .= chr($randnum+61);
}
}
return $randstr;
}  // I have added closing curly braces. I think you forgot to put.

Thanks.
no problem, im glad id of any help.



br

Author

Commented:
Thank you sumi for your help. Thanks a lot.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.