附近小店
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

rand.go 1.6 KiB

2 ay önce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package utils
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. "math/rand"
  8. "time"
  9. )
  10. func RandString(l int, c ...string) string {
  11. var (
  12. chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  13. str string
  14. num *big.Int
  15. )
  16. if len(c) > 0 {
  17. chars = c[0]
  18. }
  19. chrLen := int64(len(chars))
  20. for len(str) < l {
  21. num, _ = crand.Int(crand.Reader, big.NewInt(chrLen))
  22. str += string(chars[num.Int64()])
  23. }
  24. return str
  25. }
  26. func RandNum() string {
  27. seed := time.Now().UnixNano() + rand.Int63()
  28. return fmt.Sprintf("%05v", rand.New(rand.NewSource(seed)).Int31n(1000000))
  29. }
  30. // x的y次方
  31. func RandPow(l int) string {
  32. var i = "1"
  33. for j := 0; j < l; j++ {
  34. i += "0"
  35. }
  36. k := StrToInt64(i)
  37. n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
  38. ls := "%0" + IntToStr(l) + "v"
  39. str := fmt.Sprintf(ls, n)
  40. //min := int(math.Pow10(l - 1))
  41. //max := int(math.Pow10(l) - 1)
  42. return str
  43. }
  44. func RandInt(min, max int) int {
  45. if min >= max || min == 0 || max == 0 {
  46. if max == 0 {
  47. max = min
  48. }
  49. return max
  50. }
  51. return rand.Intn(max-min) + min
  52. }
  53. func RandInt1(min, max int) int {
  54. rand.Seed(time.Now().UnixNano())
  55. return rand.Intn(max-min+1) + min
  56. }
  57. func CalculateDistance(lat1, lng1, lat2, lng2 float64) float64 {
  58. radius := 6371.0 // 地球半径(单位:公里)
  59. dLat := DegToRad(lat2 - lat1)
  60. dLng := DegToRad(lng2 - lng1)
  61. a := math.Sin(dLat/2)*math.Sin(dLat/2) + math.Cos(DegToRad(lat1))*math.Cos(DegToRad(lat2))*math.Sin(dLng/2)*math.Sin(dLng/2)
  62. c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
  63. distance := radius * c
  64. return distance
  65. }
  66. // 将角度转换为弧度
  67. func DegToRad(deg float64) float64 {
  68. return deg * (math.Pi / 180)
  69. }