Random.php 4.6 KB

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