I fix it by :

var w = weighted.NewRandW()

func WeightRandom(services []*registry.Service) Next{
for _, service := range services {
for _,n := range service.Nodes {
w.Add(n,n.Weight)
}
}
return func() (*registry.Node, error) {
n := w.Next()
no, ok := n.(*registry.Node)
if !ok {
return nil,errors.New("get next err")
}
return no, nil
}
}

thx

在 2019年6月27日星期四 UTC+8上午11:16:01,Chou Yan写道:
>
> The situation I am currently experiencing is that it only occur on my 
> online app. I try it at local but I can not got the result. 
> haha... I make a mistake.
> the code is :
>
> func WeightRandom(services []*registry.Service) Next{
> w := weighted.NewRandW()
> for _, service := range services {
> for _,n := range service.Nodes {
> w.Add(n,n.Weight)
> }
> }
> return func() (*registry.Node, error) {
> n := w.Next()
> no, ok := n.(*registry.Node)
> if !ok {
> return nil,errors.New("get next err")
> }
> return no, nil
> }
> }
>
> The 'rand.New(...SEED)' is outside the loop of for But I neglect that this 
> func 'WeightRandom' will be mass goroutine call. so each time.Now().Unix() 
> ,the same seed ,the same sequence. So I got mass common rand number.But 
> when I change it as time.Now().UnixNano(), the seed will not be the same 
> because of the unixnano. so I got correctly. thank you very much
>
> 在 2019年6月27日星期四 UTC+8上午10:52:56,Burak Serdar写道:
>>
>> On Wed, Jun 26, 2019 at 8:48 PM Chou Yan <yan.ch...@gmail.com> wrote: 
>> > 
>> > like this: 
>> > r:=rand.New(rand.NewSource(time.Now().Unix())) 
>> > for { 
>> >     r.Intn(96) 
>> > } 
>> > I know the same seed will generate the same sequence. But I don't know 
>> why it generate mass same number when I use seed of 'time.Now().Unix()', 
>> But when I use seed of 'time.Now().UnixNano()', It will not. 
>> > I suspect what this is related to the seqence rand algorithm,But I have 
>> no relevant evidence 
>>
>> This doesn't make sense. Can you reproduce it in the go playground? 
>>
>> > 
>> > 在 2019年6月27日星期四 UTC+8上午10:32:38,Burak Serdar写道: 
>> >> 
>> >> On Wed, Jun 26, 2019 at 8:17 PM Chou Yan <yan.ch...@gmail.com> wrote: 
>> >> > 
>> >> > I use: 
>> >> > r:=rand.New(rand.NewSource(time.Now().Unix())) 
>> >> > r..Intn(96) 
>> >> 
>> >> How are you generating multiple random numbers? If your loop that 
>> >> generates these numbers include the r:=rand.New(...), then you're 
>> >> essentially seeding the random number generator with the same number, 
>> >> because the program ends before time.Now().Unix() returns a different 
>> >> value every second. 
>> >> 
>> >> Move the r:=rand.New(...) outsite the for loop. 
>> >> 
>> >> > I got: 
>> >> > 
>> >> > and val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 39 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > rand val: 82 
>> >> > ... 
>> >> > mass common sequential number . 
>> >> > But if I change that: 
>> >> > r:=rand.New(rand.NewSource(time.Now().UnixNano())) 
>> >> > It will not happen. 
>> >> > Why does this happened? and how do I choose it between 
>> time.Now().Unix() or UnixNano()? 
>> >> > 
>> >> > -- 
>> >> > 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 golan...@googlegroups.com. 
>> >> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/ce0671c5-f2df-42d9-a7af-bdef939e6d6d%40googlegroups.com.
>>  
>>
>> >> > For more options, visit https://groups.google.com/d/optout. 
>> > 
>> > -- 
>> > 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 golan...@googlegroups.com. 
>> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/99351298-7843-4556-b422-8635eb96c936%40googlegroups.com.
>>  
>>
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>

-- 
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/135ee1e4-98c5-4476-9471-4172c743c4b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to