A5下载文章资讯

分类分类

php 生成随机数函数分享

2015-05-29 14:45作者:sa

/**

* 取得随机数

*

* @param int $length 生成随机数的长度

* @param int $numeric 是否只产生数字随机数 1是0否

* @return string

*/

function random($length, $numeric = 0) {

$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);

$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));

$hash = '';

$max = strlen($seed) - 1;

for($i = 0; $i < $length; $i++) {

$hash .= $seed{mt_rand(0, $max)};

}

return $hash;

}

 

展开全部

相关

说两句网友评论
    我要跟贴
    取消