蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

27 lines
660 B

  1. package utils
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. // RED_PACKET_MIN_MONEY 红包最小金额(单位:分)
  7. const RED_PACKET_MIN_MONEY = 1
  8. // DoubleAverage 二倍均值算法
  9. func DoubleAverage(count, amount int64) int64 {
  10. if count == 1 {
  11. return amount
  12. }
  13. //计算出最大可用金额
  14. max := amount - RED_PACKET_MIN_MONEY*count
  15. //计算出最大可用平均值
  16. avg := max / count
  17. //二倍均值基础上再加上最小金额 防止出现金额为0
  18. avg2 := 2*avg + RED_PACKET_MIN_MONEY
  19. //随机红包金额序列元素,把二倍均值作为随机的最大数
  20. rand.Seed(time.Now().UnixNano())
  21. x := rand.Int63n(avg2) + RED_PACKET_MIN_MONEY
  22. return x
  23. }