diff --git a/services/red_envelope/red_envelop.go b/services/red_envelope/red_envelop.go index fd9bffd..659ce31 100644 --- a/services/red_envelope/red_envelop.go +++ b/services/red_envelope/red_envelop.go @@ -50,7 +50,7 @@ const ( ) func GenRedPack(amount, num int) []int { - r := rand.New(rand.NewSource(time.Now().UnixNano())) + r := rand.New(rand.NewSource(time.Now().Unix())) sumMoney := 0 redPacks := make([]int, 0) for i := 0; i < num; i++ { @@ -82,7 +82,7 @@ func GenRedPack(amount, num int) []int { func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnvelopeRule) error { // 判断红包是否存在 // 根据算法提前生成红包 - r := rand.New(rand.NewSource(time.Now().UnixNano())) + r := rand.New(rand.NewSource(time.Now().Unix())) if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包 // 检测红包是否存在 randSum := int(rule.RandSum * 100) diff --git a/utils/utils.go b/utils/utils.go index 792c5f8..5ff6e77 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -114,8 +114,8 @@ func RandomStr(length int) string { str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) var result []byte - r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < length; i++ { + r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond()))) result = append(result, bytes[r.Intn(len(bytes))]) } return string(result) @@ -125,8 +125,9 @@ func RandomInt(length int) int { str := "0123456789" bytes := []byte(str) var result []byte - r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < length; { + r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond()))) b := bytes[r.Intn(len(bytes))] if i == 0 && b == '0' { continue