<?Php
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);
?>
STRING REFERENCEhow to change all the letters to upper case
arman patel | 26-05-2009 |
very comprencive and use full tutorial for me thx. |
Harry | 29-07-2009 |
WOW thanks for this! |
bright-tutorials.com | 31-12-2009 |
This is another method to do it, but there are others. Good job using mt_rand not many use it! |
ketan | 08-03-2010 |
good code for beginner |
sattar | 02-10-2012 |
easy and simple tutor. are available here |
Php Programmer | 10-08-2017 |
Great!! This tutorial was really helpful to create a random number and alphabets. Simple but effective. Thanks. |