SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

String with random number and alphabets


We can generate a set of random digits by combining a set of alphabets and numbers. After this our random string will have a random combination of alphabets and numbers. You can read how to generate random numbers by reading math random generator and how to generate random alphabets here .

We will use these two methods to generate random numbers and random characters. We will add ( or combine ) these two to get our random string. Here to get a combination we will use random methods so the positions of numbers and alphabets are not fixed.

Using this you can generate random password strings for any application ( like login script etc )

Now we will keep the script inside a function and use it when required. Here we will make it flexible by accepting one input to the function by which we will tell how many digits are required. So the length of the random string can be managed based on the script design.

Here is the full function with detail explanation with it ( read the comments )

<?
function random_generator($digits){
srand ((double) microtime() * 10000000);
//Array of alphabets
$input = array ("A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");

$random_generator="";// Initialize the string to store random numbers
for($i=1;$i<$digits+1;$i++){ // Loop the number of times of required digits

if(rand(1,2) == 1){// to decide the digit should be numeric or alphabet
// Add one random alphabet
$rand_index = array_rand($input);
$random_generator .=$input[$rand_index]; // One char is added

}else{

// Add one numeric digit between 1 and 10
$random_generator .=rand(1,10); // one number is added
} // end of if else

} // end of for loop

return $random_generator;
} // end of function


echo random_generator(10);

?>


arman patel26-05-2009
very comprencive and use full tutorial for me thx.
Harry29-07-2009
WOW thanks for this!
bright-tutorials.com31-12-2009
This is another method to do it, but there are others. Good job using mt_rand not many use it!
ketan08-03-2010
good code for beginner
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
String Functions
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.