RandomUtil.php 596 B

1234567891011121314151617181920212223242526
  1. <?php
  2. namespace addons\JoinPay\common\logic\util;
  3. /**
  4. * 随机数生成工具类
  5. * Class RandomUtil
  6. * @package utils
  7. */
  8. class RandomUtil {
  9. const CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  10. /**
  11. * 生成指定长度的随机字符串
  12. * @entity int $length
  13. * @return string
  14. */
  15. public static function randomStr( $length = 16 ) {
  16. $str = "";
  17. $strLen = strlen(static::CHARS) - 1;
  18. for ($i = 0; $i < $length; $i++) {
  19. $str .= static::CHARS[mt_rand(0, $strLen)];
  20. }
  21. return $str;
  22. }
  23. }