首页 > php教程 > php手册 > php生成16位只有数字随机数,php16位随机数

php生成16位只有数字随机数,php16位随机数

WBOY
发布: 2016-06-13 09:25:34
原创
1592 人浏览过

php生成16位只有数字随机数,php16位随机数

php生成16位只有数字随机数

分享一个php生成16位随机数的代码,php生成随机数的二种方法。
方法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>;
登录后复制

方法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>
登录后复制

 

一串完整的php代码+html php随机生成10位数字,而且永远不会重复,在一个单行文本框内输出

$result=random(10);//生成10位随机数
//$result=random(10, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');//生成10位字母数字混合字符串
echo "";
/**
* 产生随机字符串
*
* @param int $length 输出长度
* @param string $chars 可选的 ,默认为 0123456789
* @return string 字符串
*/
function random($length, $chars = '0123456789') {
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i $hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
?>
 

一个生成四位随机数的PHP代码

纯数字的四位随机数
rand(1000,9999)
数字和字符混搭的四位随机字符串:

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 {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
echo GetRandStr(4);
 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板