説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Random.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 随机生成类
  13. // +----------------------------------------------------------------------
  14. namespace util;
  15. class Random
  16. {
  17. /**
  18. * 生成数字和字母含大小写
  19. *
  20. * @param int $len 长度
  21. * @param string $addChars 额外
  22. * @return string
  23. */
  24. public static function alnum($len = 6, $addChars = '')
  25. {
  26. return self::build('alnum', $len, $addChars);
  27. }
  28. /**
  29. * 生成字母含大小写
  30. *
  31. * @param int $len 长度
  32. * @param string $addChars 额外
  33. * @return string
  34. */
  35. public static function alpha($len = 6, $addChars = '')
  36. {
  37. return self::build('alpha', $len, $addChars);
  38. }
  39. /**
  40. * 生成指定长度的随机数字
  41. *
  42. * @param int $len 长度
  43. * @param string $addChars 额外
  44. * @return string
  45. */
  46. public static function numeric($len = 4, $addChars = '')
  47. {
  48. return self::build('numeric', $len, $addChars);
  49. }
  50. /**
  51. * 生成指定长度的随机汉字
  52. *
  53. * @param int $len 长度
  54. * @param string $addChars 额外
  55. * @return string
  56. */
  57. public static function chinese($len = 4, $addChars = '')
  58. {
  59. return self::build('chinese', $len, $addChars);
  60. }
  61. /**
  62. * 能用的随机数生成
  63. * @param string $type 类型 alpha/alnum/numeric/chinese/unique/md5/encrypt/sha1
  64. * @param int $len 长度
  65. * @param string $addChars 额外
  66. * @return string
  67. */
  68. public static function build($type = 'alnum', $len = 8, $addChars = '')
  69. {
  70. switch ($type) {
  71. case 'alpha':
  72. case 'alnum':
  73. case 'numeric':
  74. case 'chinese':
  75. switch ($type) {
  76. case 'alpha':
  77. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
  78. break;
  79. case 'alnum':
  80. $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
  81. break;
  82. case 'numeric':
  83. $chars = str_repeat('0123456789', 3);
  84. break;
  85. case 'chinese':
  86. $chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书" . $addChars;
  87. break;
  88. }
  89. if ($len > 10) {
  90. $chars = $type == 'numeric' ? str_repeat($chars, $len) : str_repeat($chars, 5);
  91. }
  92. if ($type != 'chinese') {
  93. $chars = str_shuffle($chars);
  94. $str = substr($chars, 0, $len);
  95. } else {
  96. for ($i = 0; $i < $len; $i++) {
  97. $str .= mb_substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
  98. }
  99. }
  100. return $str;
  101. case 'unique':
  102. case 'md5':
  103. return md5(uniqid(mt_rand()));
  104. case 'encrypt':
  105. case 'sha1':
  106. return sha1(uniqid(mt_rand(), true));
  107. }
  108. }
  109. /**
  110. * 根据数组元素的概率获得键名
  111. *
  112. * @param array $ps array('p1'=>20, 'p2'=>30, 'p3'=>50);
  113. * @param int $num 默认为1,即随机出来的数量
  114. * @param bool $unique 默认为true,即当num>1时,随机出的数量是否唯一
  115. * @return array|int|int[]|string|string[] 当num为1时返回键名,反之返回一维数组
  116. */
  117. public static function lottery($ps, $num = 1, $unique = true)
  118. {
  119. if (!$ps) {
  120. return $num == 1 ? '' : [];
  121. }
  122. if ($num >= count($ps) && $unique) {
  123. $res = array_keys($ps);
  124. return $num == 1 ? $res[0] : $res;
  125. }
  126. $max_exp = 0;
  127. $res = [];
  128. foreach ($ps as $key => $value) {
  129. $value = substr($value, 0, stripos($value, ".") + 6);
  130. $exp = strlen(strchr($value, '.')) - 1;
  131. if ($exp > $max_exp) {
  132. $max_exp = $exp;
  133. }
  134. }
  135. $pow_exp = pow(10, $max_exp);
  136. if ($pow_exp > 1) {
  137. reset($ps);
  138. foreach ($ps as $key => $value) {
  139. $ps[$key] = $value * $pow_exp;
  140. }
  141. }
  142. $pro_sum = array_sum($ps);
  143. if ($pro_sum < 1) {
  144. return $num == 1 ? '' : [];
  145. }
  146. for ($i = 0; $i < $num; $i++) {
  147. $rand_num = mt_rand(1, $pro_sum);
  148. reset($ps);
  149. foreach ($ps as $key => $value) {
  150. if ($rand_num <= $value) {
  151. break;
  152. } else {
  153. $rand_num -= $value;
  154. }
  155. }
  156. if ($num == 1) {
  157. $res = $key;
  158. break;
  159. } else {
  160. $res[$i] = $key;
  161. }
  162. if ($unique) {
  163. $pro_sum -= $value;
  164. unset($ps[$key]);
  165. }
  166. }
  167. return $res;
  168. }
  169. /**
  170. * 获取全球唯一标识
  171. * @return string
  172. */
  173. public static function uuid()
  174. {
  175. return sprintf(
  176. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  177. mt_rand(0, 0xffff),
  178. mt_rand(0, 0xffff),
  179. mt_rand(0, 0xffff),
  180. mt_rand(0, 0x0fff) | 0x4000,
  181. mt_rand(0, 0x3fff) | 0x8000,
  182. mt_rand(0, 0xffff),
  183. mt_rand(0, 0xffff),
  184. mt_rand(0, 0xffff)
  185. );
  186. }
  187. }