英文:
Why does math/rand return the same result every time I restart my program?
问题
我有点困惑。我有一个随机字符串生成器,以下是代码:
package utils
import (
"fmt"
"math/rand"
)
var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func RandSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
fmt.Println(string(b))
return string(b)
}
每次运行这段代码时,它都会生成一个新的随机字符串,但是如果我重新启动服务器,结果会重复。以下是一些结果:
go run main.go
fpllngzieyoh43e0133ols6k1hh2gdny
xxvi7hvszwk1b182tvjzjpezi4hx9gvm
kir0xcta0opsb5qipjzb3h3x9kcegta5
m1zcv5drxckn42gb50anxndsckjdwgfw
5japz01zicapy9eqixuc9uehq235v48c
51wgg1gypq4s9miwn1dxkjqd614m58f0
fyy29g6ujmxbouxshy2plmkmhlnmdbfh
f7kq8u26873eql4yyp7fyilbb72nrtlc
go run main.go
fpllngzieyoh43e0133ols6k1hh2gdny
xxvi7hvszwk1b182tvjzjpezi4hx9gvm
kir0xcta0opsb5qipjzb3h3x9kcegta5
m1zcv5drxckn42gb50anxndsckjdwgfw
5japz01zicapy9eqixuc9uehq235v48c
有任何想法为什么会发生这种情况吗?
英文:
I'm a little confused. I have a random string generator, here's the code:
package utils
import (
"fmt"
"math/rand"
)
var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func RandSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
fmt.Println(string(b))
return string(b)
}
Every time I run this, it will generate a new random string, but if I restart the server the results will repeat themselves. Here are some results:
go run main.go
fpllngzieyoh43e0133ols6k1hh2gdny
xxvi7hvszwk1b182tvjzjpezi4hx9gvm
kir0xcta0opsb5qipjzb3h3x9kcegta5
m1zcv5drxckn42gb50anxndsckjdwgfw
5japz01zicapy9eqixuc9uehq235v48c
51wgg1gypq4s9miwn1dxkjqd614m58f0
fyy29g6ujmxbouxshy2plmkmhlnmdbfh
f7kq8u26873eql4yyp7fyilbb72nrtlc
go run main.go
fpllngzieyoh43e0133ols6k1hh2gdny
xxvi7hvszwk1b182tvjzjpezi4hx9gvm
kir0xcta0opsb5qipjzb3h3x9kcegta5
m1zcv5drxckn42gb50anxndsckjdwgfw
5japz01zicapy9eqixuc9uehq235v48c
Any idea why this is happening?
答案1
得分: 8
你需要在生成任何随机数之前,先对随机数生成器进行种子。一个好的值可以使用当前的Unix时间戳:
import (
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
}
请注意,你应该只在启动时(或分叉后)对随机数生成器进行一次种子操作,而不是在每次生成随机数之前都进行种子操作。
英文:
You need to Seed the random number generator prior to generating any random numbers. A good value to use is the current unix timestamp:
import (
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
}
Note that you should only seed the RNG once at startup (or after forking), not before each random number generation.
答案2
得分: 2
如果未设置rand.Seed
,它会将种子默认设置为1
,导致每次生成相同的随机数。
在启动时使用一个随机数种子来初始化rand
,例如rand.Seed(time.Now().UnixNano())
,将使得每次运行程序时都得到不同的结果。
你也可以创建自己的随机变量,例如:
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
func main() {
num := random.Intn(10)
fmt.Println(num)
num = random.Intn(10)
fmt.Println(num)
}
英文:
If rand.Seed
is not set, it defaults the seed to 1
, giving you the same random number every time.
Seeding rand
at startup with a random number like rand.Seed(time.Now().UnixNano())
will give you a changing result each time you run your program.
You can also create your own random var like:
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
func main() {
num := random.Intn(10)
fmt.Println(num)
num = random.Intn(10)
fmt.Println(num)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论