New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: fix probabilities in shuffleByWeight #7098
Labels
Milestone
Comments
Actually, this is more subtle. It's not a fencepost error as I had thought just looking at the base case. There is a whole class of degenerate results when the sum of the weights is small. Naturally, the uniformity of the results improves measurable once the sum of the weights increases to ~100. Probably the best thing is to add a weight multiplier to minimize the error when the sum of weights is small. |
Disregard my comment about degenerate results - I jumped to the wrong conclusion. I believe this is the correct fix is this: diff --git a/src/pkg/net/dnsclient.go b/src/pkg/net/dnsclient.go --- a/src/pkg/net/dnsclient.go +++ b/src/pkg/net/dnsclient.go @@ -191,10 +191,10 @@ } for sum > 0 && len(addrs) > 1 { s := 0 - n := rand.Intn(sum + 1) + n := rand.Intn(sum) for i := range addrs { s += int(addrs[i].Weight) - if s >= n { + if s > n { if i > 0 { t := addrs[i] copy(addrs[1:i+1], addrs[0:i]) I attached a test case or two that satisfied me. Attachments:
|
CL https://golang.org/cl/88900044 mentions this issue. |
This issue was closed by revision c45392b. Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: