Home >Backend Development >PHP Tutorial >A PHP code to generate 16-digit random numbers (two methods), 16-digit random numbers_PHP tutorial
A php code to generate a 16-digit random number, php to generate a random number Two ways to count.
Method 1
$a = mt_rand(10000000,99999999);
$b = mt_rand(10000000,99999999);
echo $a.$b;
Method 2:
$a = range(0,9);
for($i=0;$i<16;$i++){
$b [] = array_rand($a);
} // www.yuju100.com
var_dump(join("",$b));
//result string(16) "0179571910024734"
Reference link:
A bit opportunistic
< ?php$a = range(0,9);for($i=0;$i<16;$i++){$b[] = array_rand($a);}var_dump(join("",$b) );//result string(16) "0179571910024734"
Pure numeric four-digit random number
rand(1000,9999)
Four-digit random string mixed with numbers and characters:
function GetRandStr($len)
{
$chars = 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", "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", "0", "1", "2",
"3", " 4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)] ;
}
return $output;
}
echo GetRandStr(4);