It's the context: // NewRandW creates a new RandW with a random object. func NewRandW() *RandW { return &RandW{r: rand.New(rand.NewSource(time.Now().Unix()))} }
n := w.Next() // Next returns next selected item. func (rw *RandW) Next() (item interface{}) { ... randomWeight := rw.r.Intn(rw.sumOfWeights) //fmt.Printf("rand val: %d\n",randomWeight) ... } I am sorry about that I did't give the context of my code. 在 2019年6月27日星期四 UTC+8上午11:22:49,Kurtis Rader写道: > > On Wed, Jun 26, 2019 at 8:18 PM Chou Yan <yan.ch...@gmail.com > <javascript:>> wrote: > >> I fix it by : >> >> var w = weighted.NewRandW() >> > > Insufficient context to understand what that does. Let alone how it > "fixes" the problem since you haven't shown us your `NewRandW()` function. > In general you should never initialize a RNG more than once unless you are > doing so to create a reproducible sequence of values. In which case you > should be initializing it with a constant value rather than a pseudo-random > value like `time.Now().Unix()`. > > -- > Kurtis Rader > Caretaker of the exceptional canines Junior and Hank > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/638a5f5b-552f-4f56-9f7f-3253fb8c2e29%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.