Home > php教程 > php手册 > php源码分析之DZX1.5随机数函数random用法

php源码分析之DZX1.5随机数函数random用法

WBOY
Release: 2016-06-13 09:01:08
Original
1094 people have browsed it

php源码分析之DZX1.5随机数函数random用法

   本文实例讲述了php源码分析之DZX1.5随机数函数random用法。分享给大家供大家参考。具体如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

* @param int $length: 随机数长度

* @param int $numeric: 0或非0,其中0表示随机数由全数字组成,非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

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

}

return $hash;

}

echo random('15',1);

echo '
';

echo random('15');

/*End of php*/

  运行结果如下:

  228283270677112

  SjqffsU76994qfS

  希望本文所述对大家的php程序设计有所帮助。

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template