Home > Backend Development > PHP Tutorial > php源码分析之DZX1.5随机数函数random用法_php技巧

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

WBOY
Release: 2016-05-16 20:13:38
Original
963 people have browsed it

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

<&#63;php
/**
 * @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 &#63; 10 : 35);
  $seed = $numeric &#63; (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;
}
echo random('15',1);
echo '<br />';
echo random('15');
/*End of php*/
Copy after login

运行结果如下:

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template