golang踩坑---批量生成随机数重复问题

lannisiter ·
搞个MAP,range不就完了
#1
更多评论
楼主虽然有了解决办法,但是还不是最完美的,这个问题已经很多人解释过了,Golang的`全局随机数种子`只需要初始化一次就能确保之后的随机数是随机的。至于`为什么重复赋值随机数种子会导致随机数重复`的原因主要是在于`time.Now().Unix()`改变的速度远远比不上`CPU的运行速度`,导致在`极短的时间`随机数种子都是一样,种子一样 = 随机数一样
#2
// Unix returns t as a Unix time, the number of seconds elapsed // since January 1, 1970 UTC. The result does not depend on the // location associated with t. // Unix-like operating systems often record time as a 32-bit // count of seconds, but since the method here returns a 64-bit // value it is valid for billions of years into the past or future. func (t Time) Unix() int64 { return t.unixSec() } // UnixNano returns t as a Unix time, the number of nanoseconds elapsed // since January 1, 1970 UTC. The result is undefined if the Unix time // in nanoseconds cannot be represented by an int64 (a date before the year // 1678 or after 2262). Note that this means the result of calling UnixNano // on the zero Time is undefined. The result does not depend on the // location associated with t. func (t Time) UnixNano() int64 { return (t.unixSec())*1e9 + int64(t.nsec()) } 写代码前就不能看看文档吗?Unix是秒级的,UnixNano才是纳秒。。。
#3