grand.go 4.8 KB

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