From 54d6c4c4a6c08a1e4d0c094e081704b714d6f5a1 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Fri, 30 Aug 2024 22:05:24 +0800 Subject: [PATCH] update --- app/hdl/hdl_demo.go | 5 +++++ app/utils/rand.go | 27 +++++++++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/hdl/hdl_demo.go b/app/hdl/hdl_demo.go index d7dfada..53b9a6e 100644 --- a/app/hdl/hdl_demo.go +++ b/app/hdl/hdl_demo.go @@ -7,10 +7,15 @@ import ( "applet/app/utils" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/super/implement" + "fmt" "github.com/gin-gonic/gin" ) func Demo(c *gin.Context) { + for { + mediumId := utils.GenerateUniqueRandomNumbers(8) + fmt.Println(mediumId) + } appId := c.DefaultQuery("app_id", "") masterId := svc.GetMasterId(c) adUnitId := c.DefaultQuery("adunit_id", "") diff --git a/app/utils/rand.go b/app/utils/rand.go index 62563c0..61869aa 100644 --- a/app/utils/rand.go +++ b/app/utils/rand.go @@ -32,22 +32,16 @@ func RandNum() string { } func GenerateUniqueRandomNumbers(n int) string { - rand.Seed(time.Now().UnixNano()) // 初始化随机种子 - numbers := make([]int, n) // 创建一个切片来保存随机数 - for i := range numbers { - j := 0 - for { - b := rand.Intn(10) // 生成0-9之间的随机数 - numbers[i] = b - for _, num := range numbers[:i] { - if num == b { - j++ - break - } - } - if j == 0 { - break - } + rand.Seed(time.Now().UnixNano() + rand.Int63()) // 初始化随机种子 + + numbers := make([]int, 0, n) // 创建一个切片来保存随机数,初始容量为n + used := make(map[int]bool) // 跟踪已使用的数字 + + for len(numbers) < n { + b := rand.Intn(10) // 生成0-9之间的随机数 + if !used[b] { + numbers = append(numbers, b) + used[b] = true } } @@ -55,5 +49,6 @@ func GenerateUniqueRandomNumbers(n int) string { for _, v := range numbers { numbersStr += strconv.Itoa(v) } + return numbersStr }