Home > Backend Development > PHP Tutorial > PHP generates 16-digit random numbers only, php16-digit random numbers_PHP tutorial

PHP generates 16-digit random numbers only, php16-digit random numbers_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:19:10
Original
1693 people have browsed it

php generates a 16-digit random number with only digits, php16-digit random number

php generates a 16-digit random number with only digits

Share a code for PHP to generate 16-digit random numbers, two methods for PHP to generate random numbers.
Method 1,

<?<span>php
</span><span>$a</span> = <span>mt_rand</span>(10000000,99999999<span>);
</span><span>$b</span> = <span>mt_rand</span>(10000000,99999999<span>);
</span><span>echo</span> <span>$a</span>.<span>$b</span>;
Copy after login

Method 2:

<?<span>php
</span><span>$a</span> = <span>range</span>(0,9<span>);
</span><span>for</span>(<span>$i</span>=0;<span>$i</span><16;<span>$i</span>++<span>){
</span><span>$b</span>[] = <span>array_rand</span>(<span>$a</span><span>);
} // www.yuju100.com
</span><span>var_dump</span>(<span>join</span>("",<span>$b</span><span>));
</span><span>//</span><span>结果string(16) "0179571910024734"</span>
Copy after login

A complete string of php code + html php randomly generates 10 digits and will never be repeated, output in a single line text box

$result=random(10);//Generate a 10-digit random number
//$result=random(10, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');//Generate a 10-digit alphanumeric mixed string
echo "";
/**
* generate random string
*
* @param int $length output length
* @param string $chars optional, default is 0123456789
* @return string string
*/
function random($length, $chars = '0123456789') {
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
?>

A PHP code that generates four-digit random numbers

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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/878339.htmlTechArticlephp generates a 16-digit random number with only digits, php16-digit random number php generates a 16-digit random number with only digits Share a php Code to generate 16-digit random numbers, two methods of generating random numbers in PHP. Method...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template