Random.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: fangx
  5. * Date: 2021/7/8
  6. * Time: 9:48
  7. */
  8. namespace App\Common\Library;
  9. /**
  10. * 随机生成类
  11. */
  12. class Random
  13. {
  14. /**
  15. * 生成数字和字母
  16. *
  17. * @param int $len 长度
  18. * @return string
  19. */
  20. public static function alnum($len = 6)
  21. {
  22. return self::build('alnum', $len);
  23. }
  24. /**
  25. * 仅生成字符
  26. *
  27. * @param int $len 长度
  28. * @return string
  29. */
  30. public static function alpha($len = 6)
  31. {
  32. return self::build('alpha', $len);
  33. }
  34. /**
  35. * 生成指定长度的随机数字
  36. *
  37. * @param int $len 长度
  38. * @return string
  39. */
  40. public static function numeric($len = 4)
  41. {
  42. return self::build('numeric', $len);
  43. }
  44. /**
  45. * 生成指定长度的无0随机数字
  46. *
  47. * @param int $len 长度
  48. * @return string
  49. */
  50. public static function nozero($len = 4)
  51. {
  52. return self::build('nozero', $len);
  53. }
  54. /**
  55. * 能用的随机数生成
  56. * @param string $type 类型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1
  57. * @param int $len 长度
  58. * @return string
  59. */
  60. public static function build($type = 'alnum', $len = 8)
  61. {
  62. switch ($type) {
  63. case 'alpha':
  64. case 'alnum':
  65. case 'numeric':
  66. case 'nozero':
  67. switch ($type) {
  68. case 'alpha':
  69. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  70. break;
  71. case 'alnum':
  72. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  73. break;
  74. case 'numeric':
  75. $pool = '0123456789';
  76. break;
  77. case 'nozero':
  78. $pool = '123456789';
  79. break;
  80. }
  81. return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
  82. case 'unique':
  83. case 'md5':
  84. return md5(uniqid(mt_rand()));
  85. case 'encrypt':
  86. case 'sha1':
  87. return sha1(uniqid(mt_rand(), true));
  88. }
  89. }
  90. /**
  91. * 根据数组元素的概率获得键名
  92. *
  93. * @param array $ps array('p1'=>20, 'p2'=>30, 'p3'=>50);
  94. * @param int $num 默认为1,即随机出来的数量
  95. * @param bool $unique 默认为true,即当num>1时,随机出的数量是否唯一
  96. * @return mixed 当num为1时返回键名,反之返回一维数组
  97. */
  98. public static function lottery($ps, $num = 1, $unique = true)
  99. {
  100. if (!$ps) {
  101. return $num == 1 ? '' : [];
  102. }
  103. if ($num >= count($ps) && $unique) {
  104. $res = array_keys($ps);
  105. return $num == 1 ? $res[0] : $res;
  106. }
  107. $max_exp = 0;
  108. $res = [];
  109. foreach ($ps as $key => $value) {
  110. $value = substr($value, 0, stripos($value, ".") + 6);
  111. $exp = strlen(strchr($value, '.')) - 1;
  112. if ($exp > $max_exp) {
  113. $max_exp = $exp;
  114. }
  115. }
  116. $pow_exp = pow(10, $max_exp);
  117. if ($pow_exp > 1) {
  118. reset($ps);
  119. foreach ($ps as $key => $value) {
  120. $ps[$key] = $value * $pow_exp;
  121. }
  122. }
  123. $pro_sum = array_sum($ps);
  124. if ($pro_sum < 1) {
  125. return $num == 1 ? '' : [];
  126. }
  127. for ($i = 0; $i < $num; $i++) {
  128. $rand_num = mt_rand(1, $pro_sum);
  129. reset($ps);
  130. foreach ($ps as $key => $value) {
  131. if ($rand_num <= $value) {
  132. break;
  133. } else {
  134. $rand_num -= $value;
  135. }
  136. }
  137. if ($num == 1) {
  138. $res = $key;
  139. break;
  140. } else {
  141. $res[$i] = $key;
  142. }
  143. if ($unique) {
  144. $pro_sum -= $value;
  145. unset($ps[$key]);
  146. }
  147. }
  148. return $res;
  149. }
  150. /**
  151. * 获取全球唯一标识
  152. * @return string
  153. */
  154. public static function uuid()
  155. {
  156. return sprintf(
  157. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  158. mt_rand(0, 0xffff),
  159. mt_rand(0, 0xffff),
  160. mt_rand(0, 0xffff),
  161. mt_rand(0, 0x0fff) | 0x4000,
  162. mt_rand(0, 0x3fff) | 0x8000,
  163. mt_rand(0, 0xffff),
  164. mt_rand(0, 0xffff),
  165. mt_rand(0, 0xffff)
  166. );
  167. }
  168. }