刘思伽 高越:产生任意长度的字符串+数字随机数

来源:百度文库 编辑:中财网 时间:2024/05/05 10:49:03

可以自定义产生什么字符串以及多长

     [PHP]代码

  function random($length)

  {

  $hash = '';

  $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';

  $max = strlen($chars) - 1;

  mt_srand((double)microtime() * 1000000);

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

  {

  $hash .= $chars[mt_rand(0, $max)];

  }

  return $hash;

  }