Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Code for generating a random string

Posted on 2011-04-27
8
Medium Priority
?
277 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;
0
Comment
Question by:krutarth941
8 Comments
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35481261
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
0
 
LVL 4

Accepted Solution

by:
Frozenice earned 2000 total points
ID: 35481274
hi,

function generateRandStr($length){
$randstr = "";
for($i=0; $i<$length; $i++){     //this loop goes on with the length pass on $length parameter
$randnum = mt_rand(0,61);    //will pick up numbers from 0 1 2 .... upto 61
if($randnum < 10){       //if the mt_rand above gets a number less than 10
$randstr .= chr($randnum+48);   //he will add a + 48 ex, 5 so the result would be 53 equivalent to chr(53) character you will find those equivalent from here http://www.asciitable.com/
}else if($randnum < 36){  //and so on and so fort.
$randstr .= chr($randnum+55);
}else{
$randstr .= chr($randnum+61);
}
}
return $randstr;


BR,
Frozenice
0
 

Author Comment

by:krutarth941
ID: 35481289
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;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:krutarth941
ID: 35481317
Thank you very much Frozenice. I got it.
0
 

Author Closing Comment

by:krutarth941
ID: 35481328
Excellent answer
0
 
LVL 2

Expert Comment

by:sumi_dev
ID: 35481339
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.
0
 
LVL 4

Expert Comment

by:Frozenice
ID: 35481343
no problem, im glad id of any help.



br
0
 

Author Comment

by:krutarth941
ID: 35481374
Thank you sumi for your help. Thanks a lot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question