grand.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package grand provides high performance random bytes/number/string generation functionality.
  7. package grand
  8. import (
  9. "encoding/binary"
  10. "time"
  11. )
  12. var (
  13. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
  14. symbols = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" // 32
  15. digits = "0123456789" // 10
  16. characters = letters + digits + symbols // 94
  17. )
  18. // Intn returns an int number which is between 0 and max: [0, max).
  19. //
  20. // Note that:
  21. // 1. The `max` can only be greater than 0, or else it returns `max` directly;
  22. // 2. The result is greater than or equal to 0, but less than `max`;
  23. // 3. The result number is 32bit and less than math.MaxUint32.
  24. func Intn(max int) int {
  25. if max <= 0 {
  26. return max
  27. }
  28. n := int(binary.LittleEndian.Uint32(<-bufferChan)) % max
  29. if (max > 0 && n < 0) || (max < 0 && n > 0) {
  30. return -n
  31. }
  32. return n
  33. }
  34. // B retrieves and returns random bytes of given length `n`.
  35. func B(n int) []byte {
  36. if n <= 0 {
  37. return nil
  38. }
  39. i := 0
  40. b := make([]byte, n)
  41. for {
  42. copy(b[i:], <-bufferChan)
  43. i += 4
  44. if i >= n {
  45. break
  46. }
  47. }
  48. return b
  49. }
  50. // N returns a random int between min and max: [min, max].
  51. // The `min` and `max` also support negative numbers.
  52. func N(min, max int) int {
  53. if min >= max {
  54. return min
  55. }
  56. if min >= 0 {
  57. return Intn(max-min+1) + min
  58. }
  59. // As `Intn` dose not support negative number,
  60. // so we should first shift the value to right,
  61. // then call `Intn` to produce the random number,
  62. // and finally shift the result back to left.
  63. return Intn(max+(0-min)+1) - (0 - min)
  64. }
  65. // S returns a random string which contains digits and letters, and its length is `n`.
  66. // The optional parameter `symbols` specifies whether the result could contain symbols,
  67. // which is false in default.
  68. func S(n int, symbols ...bool) string {
  69. if n <= 0 {
  70. return ""
  71. }
  72. var (
  73. b = make([]byte, n)
  74. numberBytes = B(n)
  75. )
  76. for i := range b {
  77. if len(symbols) > 0 && symbols[0] {
  78. b[i] = characters[numberBytes[i]%94]
  79. } else {
  80. b[i] = characters[numberBytes[i]%62]
  81. }
  82. }
  83. return string(b)
  84. }
  85. // D returns a random time.Duration between min and max: [min, max].
  86. func D(min, max time.Duration) time.Duration {
  87. multiple := int64(1)
  88. if min != 0 {
  89. for min%10 == 0 {
  90. multiple *= 10
  91. min /= 10
  92. max /= 10
  93. }
  94. }
  95. n := int64(N(int(min), int(max)))
  96. return time.Duration(n * multiple)
  97. }
  98. // Str randomly picks and returns `n` count of chars from given string `s`.
  99. // It also supports unicode string like Chinese/Russian/Japanese, etc.
  100. func Str(s string, n int) string {
  101. if n <= 0 {
  102. return ""
  103. }
  104. var (
  105. b = make([]rune, n)
  106. runes = []rune(s)
  107. )
  108. if len(runes) <= 255 {
  109. numberBytes := B(n)
  110. for i := range b {
  111. b[i] = runes[int(numberBytes[i])%len(runes)]
  112. }
  113. } else {
  114. for i := range b {
  115. b[i] = runes[Intn(len(runes))]
  116. }
  117. }
  118. return string(b)
  119. }
  120. // Digits returns a random string which contains only digits, and its length is `n`.
  121. func Digits(n int) string {
  122. if n <= 0 {
  123. return ""
  124. }
  125. var (
  126. b = make([]byte, n)
  127. numberBytes = B(n)
  128. )
  129. for i := range b {
  130. b[i] = digits[numberBytes[i]%10]
  131. }
  132. return string(b)
  133. }
  134. // Letters returns a random string which contains only letters, and its length is `n`.
  135. func Letters(n int) string {
  136. if n <= 0 {
  137. return ""
  138. }
  139. var (
  140. b = make([]byte, n)
  141. numberBytes = B(n)
  142. )
  143. for i := range b {
  144. b[i] = letters[numberBytes[i]%52]
  145. }
  146. return string(b)
  147. }
  148. // Symbols returns a random string which contains only symbols, and its length is `n`.
  149. func Symbols(n int) string {
  150. if n <= 0 {
  151. return ""
  152. }
  153. var (
  154. b = make([]byte, n)
  155. numberBytes = B(n)
  156. )
  157. for i := range b {
  158. b[i] = symbols[numberBytes[i]%32]
  159. }
  160. return string(b)
  161. }
  162. // Perm returns, as a slice of n int numbers, a pseudo-random permutation of the integers [0,n).
  163. // TODO performance improving for large slice producing.
  164. func Perm(n int) []int {
  165. m := make([]int, n)
  166. for i := 0; i < n; i++ {
  167. j := Intn(i + 1)
  168. m[i] = m[j]
  169. m[j] = i
  170. }
  171. return m
  172. }
  173. // Meet randomly calculate whether the given probability `num`/`total` is met.
  174. func Meet(num, total int) bool {
  175. return Intn(total) < num
  176. }
  177. // MeetProb randomly calculate whether the given probability is met.
  178. func MeetProb(prob float32) bool {
  179. return Intn(1e7) < int(prob*1e7)
  180. }