From bd6925f2f1961203bd4f832350b8b29bbcdd0641 Mon Sep 17 00:00:00 2001 From: fumiama Date: Fri, 18 Feb 2022 20:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=F0=9F=94=A5=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=20seed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 3 +++ plugin_aiwife/non-existent.go | 2 -- plugin_diana/bing.go | 4 ---- plugin_fortune/fortune.go | 10 +++++----- plugin_gif/run.go | 3 --- plugin_manager/manager.go | 1 - plugin_vtb_quotation/model/model.go | 2 -- 7 files changed, 8 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 9f1bdf3d..ce36bd51 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package main import ( "flag" "fmt" + "math/rand" "os" "strings" + "time" // ---------以下插件均可通过前面加 // 注释,注释后停用并不加载插件--------- // // ----------------------插件优先级按顺序从高到低---------------------- // @@ -220,6 +222,7 @@ func getKanban() string { func main() { order.Wait() printBanner() + rand.Seed(time.Now().UnixNano()) // 全局 seed,其他插件无需再 seed // 帮助 zero.OnFullMatchGroup([]string{"/help", ".help", "菜单"}, zero.OnlyToMe).SetBlock(true). Handle(func(ctx *zero.Ctx) { diff --git a/plugin_aiwife/non-existent.go b/plugin_aiwife/non-existent.go index d9cb2800..e758a956 100644 --- a/plugin_aiwife/non-existent.go +++ b/plugin_aiwife/non-existent.go @@ -4,7 +4,6 @@ package aiwife import ( "fmt" "math/rand" - "time" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" @@ -19,7 +18,6 @@ const ( ) func init() { // 插件主体 - rand.Seed(time.Now().UnixNano()) control.Register("aiwife", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "AIWife\n" + diff --git a/plugin_diana/bing.go b/plugin_diana/bing.go index c719059d..26381265 100644 --- a/plugin_diana/bing.go +++ b/plugin_diana/bing.go @@ -2,9 +2,6 @@ package diana import ( - "math/rand" - "time" - zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -36,7 +33,6 @@ func init() { // 随机发送一篇上面的小作文 engine.OnFullMatch("小作文").SetBlock(true). Handle(func(ctx *zero.Ctx) { - rand.Seed(time.Now().UnixNano()) // 绕过第一行发病 ctx.SendChain(message.Text(data.RandText())) }) diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 3f5c9b61..6f1b3895 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -188,8 +188,8 @@ func randimage(path string, seed int64) (im image.Image, index int, err error) { } defer reader.Close() - rand.Seed(seed) - index = rand.Intn(len(reader.File)) + r := rand.New(rand.NewSource(seed)) + index = r.Intn(len(reader.File)) file := reader.File[index] f, err := file.Open() if err != nil { @@ -206,9 +206,9 @@ func randimage(path string, seed int64) (im image.Image, index int, err error) { // @param seed 随机数种子 // @return 签名 & 签文 & 错误信息 func randtext(seed int64) (string, string) { - rand.Seed(seed) - r := rand.Intn(len(omikujis)) - return omikujis[r]["title"], omikujis[r]["content"] + r := rand.New(rand.NewSource(seed)) + i := r.Intn(len(omikujis)) + return omikujis[i]["title"], omikujis[i]["content"] } // @function draw 绘制运势图 diff --git a/plugin_gif/run.go b/plugin_gif/run.go index b6268c0a..f65acd20 100644 --- a/plugin_gif/run.go +++ b/plugin_gif/run.go @@ -2,11 +2,9 @@ package gif import ( - "math/rand" "reflect" "strconv" "strings" - "time" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" @@ -24,7 +22,6 @@ var ( ) func init() { // 插件主体 - rand.Seed(time.Now().UnixNano()) // 设置种子 en := control.Register("gif", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "制图\n- " + strings.Join(cmds, "\n- "), diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index e617f80f..d2dc4afb 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -342,7 +342,6 @@ func init() { // 插件主体 return temp[i].Get("last_sent_time").Int() < temp[j].Get("last_sent_time").Int() }) temp = temp[math.Max(0, len(temp)-10):] - rand.Seed(time.Now().UnixNano()) who := temp[rand.Intn(len(temp))] if who.Get("user_id").Int() == ctx.Event.SelfID { ctx.SendChain(message.Text("幸运儿居然是我自己")) diff --git a/plugin_vtb_quotation/model/model.go b/plugin_vtb_quotation/model/model.go index ba77a25b..ff87e65f 100644 --- a/plugin_vtb_quotation/model/model.go +++ b/plugin_vtb_quotation/model/model.go @@ -8,7 +8,6 @@ import ( "os" "strconv" "strings" - "time" "github.com/FloatTech/zbputils/web" "github.com/jinzhu/gorm" @@ -160,7 +159,6 @@ func (vdb *VtbDB) GetThirdCategory(firstIndex, secondIndex, thirdIndex int) Thir // RandomVtb ... func (vdb *VtbDB) RandomVtb() ThirdCategory { db := (*gorm.DB)(vdb) - rand.Seed(time.Now().UnixNano()) var count int var tc ThirdCategory db.Model(&ThirdCategory{}).Count(&count).Offset(rand.Intn(count)).Take(&tc)