grand.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). 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. "unsafe"
  11. )
  12. var (
  13. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
  14. symbols = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" // 32
  15. digits = "0123456789" // 10
  16. characters = letters + digits + symbols // 94
  17. )
  18. // Intn returns a 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. // Because Intn dose not support negative number,
  58. // so we should first shift the value to left,
  59. // then call Intn to produce the random number,
  60. // and finally shift the result back to right.
  61. return Intn(max-(min-0)+1) + (min - 0)
  62. }
  63. if min < 0 {
  64. // Because Intn dose not support negative number,
  65. // so we should first shift the value to right,
  66. // then call Intn to produce the random number,
  67. // and finally shift the result back to left.
  68. return Intn(max+(0-min)+1) - (0 - min)
  69. }
  70. return 0
  71. }
  72. // S returns a random string which contains digits and letters, and its length is <n>.
  73. // The optional parameter <symbols> specifies whether the result could contain symbols,
  74. // which is false in default.
  75. func S(n int, symbols ...bool) string {
  76. if n <= 0 {
  77. return ""
  78. }
  79. var (
  80. b = make([]byte, n)
  81. numberBytes = B(n)
  82. )
  83. for i := range b {
  84. if len(symbols) > 0 && symbols[0] {
  85. b[i] = characters[numberBytes[i]%94]
  86. } else {
  87. b[i] = characters[numberBytes[i]%62]
  88. }
  89. }
  90. return *(*string)(unsafe.Pointer(&b))
  91. }
  92. // Str randomly picks and returns <n> count of chars from given string <s>.
  93. // It also supports unicode string like Chinese/Russian/Japanese, etc.
  94. func Str(s string, n int) string {
  95. if n <= 0 {
  96. return ""
  97. }
  98. var (
  99. b = make([]rune, n)
  100. runes = []rune(s)
  101. )
  102. if len(runes) <= 255 {
  103. numberBytes := B(n)
  104. for i := range b {
  105. b[i] = runes[int(numberBytes[i])%len(runes)]
  106. }
  107. } else {
  108. for i := range b {
  109. b[i] = runes[Intn(len(runes))]
  110. }
  111. }
  112. return string(b)
  113. }
  114. // Digits returns a random string which contains only digits, and its length is <n>.
  115. func Digits(n int) string {
  116. if n <= 0 {
  117. return ""
  118. }
  119. var (
  120. b = make([]byte, n)
  121. numberBytes = B(n)
  122. )
  123. for i := range b {
  124. b[i] = digits[numberBytes[i]%10]
  125. }
  126. return *(*string)(unsafe.Pointer(&b))
  127. }
  128. // Letters returns a random string which contains only letters, and its length is <n>.
  129. func Letters(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] = letters[numberBytes[i]%52]
  139. }
  140. return *(*string)(unsafe.Pointer(&b))
  141. }
  142. // Symbols returns a random string which contains only symbols, and its length is <n>.
  143. func Symbols(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] = symbols[numberBytes[i]%32]
  153. }
  154. return *(*string)(unsafe.Pointer(&b))
  155. }
  156. // Perm returns, as a slice of n int numbers, a pseudo-random permutation of the integers [0,n).
  157. // TODO performance improving for large slice producing.
  158. func Perm(n int) []int {
  159. m := make([]int, n)
  160. for i := 0; i < n; i++ {
  161. j := Intn(i + 1)
  162. m[i] = m[j]
  163. m[j] = i
  164. }
  165. return m
  166. }
  167. // Meet randomly calculate whether the given probability <num>/<total> is met.
  168. func Meet(num, total int) bool {
  169. return Intn(total) < num
  170. }
  171. // MeetProb randomly calculate whether the given probability is met.
  172. func MeetProb(prob float32) bool {
  173. return Intn(1e7) < int(prob*1e7)
  174. }