0
Golang1.21废弃的方法--random.seed
source link: https://zhangyiming748.github.io/post/golang121_readom_seed/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Golang1.21废弃的方法--random.seed
2023-09-29 Golang
新的方式如下
func RandomWithSeed() {
rand.Seed(time.Now().Unix())
a := rand.Intn(2000)
seed := rand.New(rand.NewSource(time.Now().Unix()))
b := seed.Intn(2000)
if a == b {
slog.Info("生成的随机数", slog.Int("a", a), slog.Int("b", b))
} else {
slog.Info("不相等")
}
}
但是经过测试,两者等效,虽然被废弃但是功能相同
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK