random_math.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package base64Captcha
  2. import (
  3. "encoding/binary"
  4. "image/color"
  5. "math"
  6. "math/rand"
  7. "strings"
  8. "time"
  9. )
  10. func init() {
  11. //init rand seed
  12. rand.Seed(time.Now().UnixNano())
  13. }
  14. //RandText creates random text of given size.
  15. func RandText(size int, sourceChars string) string {
  16. if sourceChars == "" || size == 0 {
  17. return ""
  18. }
  19. if size >= len(sourceChars) {
  20. sourceChars = strings.Repeat(sourceChars, size)
  21. }
  22. sourceRunes := []rune(sourceChars)
  23. sourceLength := len(sourceRunes)
  24. text := make([]rune, size)
  25. for i := range text {
  26. text[i] = sourceRunes[rand.Intn(sourceLength)]
  27. }
  28. return string(text)
  29. }
  30. //Random get random number between min and max. 生成指定大小的随机数.
  31. func random(min int64, max int64) float64 {
  32. return float64(min) + rand.Float64()*float64(max-min)
  33. }
  34. //RandDeepColor get random deep color. 随机生成深色系.
  35. func RandDeepColor() color.RGBA {
  36. randColor := RandColor()
  37. increase := float64(30 + rand.Intn(255))
  38. red := math.Abs(math.Min(float64(randColor.R)-increase, 255))
  39. green := math.Abs(math.Min(float64(randColor.G)-increase, 255))
  40. blue := math.Abs(math.Min(float64(randColor.B)-increase, 255))
  41. return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
  42. }
  43. //RandLightColor get random ligth color. 随机生成浅色.
  44. func RandLightColor() color.RGBA {
  45. red := rand.Intn(55) + 200
  46. green := rand.Intn(55) + 200
  47. blue := rand.Intn(55) + 200
  48. return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
  49. }
  50. //RandColor get random color. 生成随机颜色.
  51. func RandColor() color.RGBA {
  52. red := rand.Intn(255)
  53. green := rand.Intn(255)
  54. var blue int
  55. if (red + green) > 400 {
  56. blue = 0
  57. } else {
  58. blue = 400 - green - red
  59. }
  60. if blue > 255 {
  61. blue = 255
  62. }
  63. return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
  64. }
  65. func randIntRange(from, to int) int {
  66. // rand.Intn panics if n <= 0.
  67. if to-from <= 0 {
  68. return from
  69. }
  70. return rand.Intn(to-from) + from
  71. }
  72. func randFloat64Range(from, to float64) float64 {
  73. return rand.Float64()*(to-from) + from
  74. }
  75. func randBytes(n int) []byte {
  76. // Since we don't have a buffer for generated bytes in siprng state,
  77. // we just generate enough 8-byte blocks and then cut the result to the
  78. // required length. Doing it this way, we lose generated bytes, and we
  79. // don't get the strictly sequential deterministic output from PRNG:
  80. // calling Uint64() and then Bytes(3) produces different output than
  81. // when calling them in the reverse order, but for our applications
  82. // this is OK.
  83. numBlocks := (n + 8 - 1) / 8
  84. b := make([]byte, numBlocks*8)
  85. for i := 0; i < len(b); i += 8 {
  86. binary.LittleEndian.PutUint64(b[i:], rand.Uint64())
  87. }
  88. return b[:n]
  89. }
  90. // RandomId returns a new random id key string.
  91. func RandomId() string {
  92. b := randomBytesMod(idLen, byte(len(idChars)))
  93. for i, c := range b {
  94. b[i] = idChars[c]
  95. }
  96. return string(b)
  97. }