-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
math/rand: document which bits of seed are used #15788
Comments
Even though functions in rand take int64 seed parameter the actual implemented algorithm uses only 31 bit of the seed ( In your example:
Probably this should be documented. |
Aha, well this information allows me to rewrite my program to fix it. Thanks :) I'll leave this open and let others decide what to do with it, maybe use it to file a documentation bug. |
Out of curiosity, why does it take an int64 opposed to an int32 when it only uses 31bits of information? |
@BlackBaronsTux The package is written in terms of generating random numbers from a |
Why is this a doc bug and "let's just change the impl to match what a reasonable person would read in the current docs?" I'm happy to pick this up as a doc bug, but it seems like it might also be reasonable to collapse the given int64 into an int32 by xor'ing the top 32bits with the bottom 32bits, so that a caller who expects all 64bits to matter will get what they expect. (It also won't change the current behavior of the default seed 1, because 0 ^ 1 == 1.) |
@jeffallen Changing the random number sequences for a given seed is painful for anybody with existing tests that expect a particular sequence. It's a change we would make to fix a clear bug, such as an unexpected lack of randomness, but it's not a change we will make for a problem like this. |
CL https://golang.org/cl/23352 mentions this issue. |
CL https://golang.org/cl/23461 mentions this issue. |
Fixes #15788 Change-Id: I5a1fd1e5992f1c16cf8d8437d742bf02e1653b9c Reviewed-on: https://go-review.googlesource.com/23461 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
go version
)?1.6.2 (1.6.0 has the same issue)
go env
)?GOHOSTARCH="amd64"
GOHOSTOS="linux"
Ubuntu 14.04.02 Trusty
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
https://play.golang.org/p/JSKCIMotEE
A different output string, and a different sequence of integers in the array.
The exact same output string, and the exact same sequence of integers in the array.
Basically using a different seed (especially one that's numerically quite different) should probably generate a different random sequence. It's very possible that that's just a limitation of the algorithm and that's life but just in case this is actually a bug I wanted to file it so at least someone with more knowledge than me could confirm it either way.
The text was updated successfully, but these errors were encountered: