已设置 rand 的种子,但输出红包金额相同
func SimpleRand(count, amount int64) int64 {
// 当红包剩余一个
if count == 1 {
return amount
}
// 计算最大可调度金额
max := amount - min*count
rand.Seed(time.Now().UnixNano())
x := rand.Int63n(max) + min
return x
}func main() {
count, amount := int64(10), int64(100)
for i := int64(0); i < count; i++ {
x := algo.SimpleRand(count, amount*100)
fmt.Print(float64(x)/float64(100), ", ")
}
fmt.Println()
}各位小伙伴,已设置 rand 的种子,最后输出的红包金额相同。
输出:32.74, 54.66, 54.66, 54.66, 54.66, 54.66, 54.66, 54.66, 54.66, 54.66,
如果设置这样
for i := int64(0); i < count; i++ {
time.Sleep(10)
x := algo.SimpleRand(count, amount*100)
fmt.Print(float64(x)/float64(100), ", ")
}红包金额才能跟老师的一样。
这是程序运行太快,时间差太小导致的吗?




