Random Alphanumeric String Generator Script in PHP
These are two functions that you can use to assign random strings of any length. You can use these for session IDs, tracking IDs, validation checks, password generators, etc.
Demonstration of randomly-generated strings, 16 characters long; refresh the page to change the values:
- u6hfh6xhgbb22m5z
- dz1syicfoqfjzuz7
- gdgst1nsfo0bq4lg
- muomvjw0f6pmgyfk
- c1yocmuiub91g8fg
The first function: assign alphanumeric value
The first function is long but simple. We put in a number, 1 to 36, and assign an alphanumeric value to it.
function assign_rand_value($num)
{
// accepts 1 - 36
switch($num)
{
case "1":
$rand_value = "a";
break;
case "2":
$rand_value = "b";
break;
case "3":
$rand_value = "c";
break;
case "4":
$rand_value = "d";
break;
case "5":
$rand_value = "e";
break;
case "6":
$rand_value = "f";
break;
case "7":
$rand_value = "g";
break;
case "8":
$rand_value = "h";
break;
case "9":
$rand_value = "i";
break;
case "10":
$rand_value = "j";
break;
case "11":
$rand_value = "k";
break;
case "12":
$rand_value = "l";
break;
case "13":
$rand_value = "m";
break;
case "14":
$rand_value = "n";
break;
case "15":
$rand_value = "o";
break;
case "16":
$rand_value = "p";
break;
case "17":
$rand_value = "q";
break;
case "18":
$rand_value = "r";
break;
case "19":
$rand_value = "s";
break;
case "20":
$rand_value = "t";
break;
case "21":
$rand_value = "u";
break;
case "22":
$rand_value = "v";
break;
case "23":
$rand_value = "w";
break;
case "24":
$rand_value = "x";
break;
case "25":
$rand_value = "y";
break;
case "26":
$rand_value = "z";
break;
case "27":
$rand_value = "0";
break;
case "28":
$rand_value = "1";
break;
case "29":
$rand_value = "2";
break;
case "30":
$rand_value = "3";
break;
case "31":
$rand_value = "4";
break;
case "32":
$rand_value = "5";
break;
case "33":
$rand_value = "6";
break;
case "34":
$rand_value = "7";
break;
case "35":
$rand_value = "8";
break;
case "36":
$rand_value = "9";
break;
}
return $rand_value;
}
The second function: generate random string
The second function is what makes this work. We feed it the length of the string we want, and it gives us a random string of alphanumeric characters that length.
Demonstration (reload to change values)
- function get_rand_id(3) gives us u7n
- function get_rand_id(50) gives us nuzmqab6iqt4x78nv057e6m57f4224g43dbu50m7nfsr2dwfs2
Here's the code of the random string generator function:
function get_rand_id($length)
{
if($length>0)
{
$rand_id="";
for($i=1; $i<=$length; $i++)
{
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,36);
$rand_id .= assign_rand_value($num);
}
}
return $rand_id;
}
Here are the steps of the code, explained:
- Check that the length is not 0.
- Start a loop. We loop once for each character we need for the string.
- We "seed" the random number function with the current microtime (times 1000000).
- We choose a random number between 1 and 36 (because our first function, assign_rand_value($num) accepts that range of numbers.
- We then use assign_rand_value($num) to change the random number into its corresponding alphanumeric number.
s
Altering the random string generator
For
strings of only numbers, keep the first function and add this function (the changes have been put in
bold)
function get_rand_numbers($length)
{
if($length>0)
{
$rand_id="";
for($i=1; $i<=$length; $i++)
{
mt_srand((double)microtime() * 1000000);
$num = mt_rand(27,36);
$rand_id .= assign_rand_value($num);
}
}
return $rand_id;
}
Demo, reload to change: - get_rand_numbers(10) gives 1606197668
- get_rand_numbers(25) gives 9573953831072589689487157
For
only letters, keep the first function and add this function (the changes have been put in
bold)
function get_rand_letters($length)
{
if($length>0)
{
$rand_id="";
for($i=1; $i<=$length; $i++)
{
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,26);
$rand_id .= assign_rand_value($num);
}
}
return $rand_id;
}
Demo, reload to change: - get_rand_letters(10) gives aivlpcxmmn
- get_rand_letters(25) gives kphodihrrkyowqxbeywqcwybm