Skip to content
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

Randomness behaviour with go 1.20 #15

Open
Bios-Marcel opened this issue Aug 26, 2023 · 0 comments
Open

Randomness behaviour with go 1.20 #15

Bios-Marcel opened this issue Aug 26, 2023 · 0 comments

Comments

@Bios-Marcel
Copy link

You've got this function calling rand.Seed for a non deterministic mode.

func NonDeterministicMode() {

However, since go 1.20, rand is automatically seeded, unless explicitly disabled:

// Prior to Go 1.20, the generator was seeded like Seed(1) at program startup.
// To force the old behavior, call Seed(1) at program startup.
// Alternately, set GODEBUG=randautoseed=0 in the environment
// before making any calls to functions in this package.

Maybe you want to factor this behaviour in, not sure. For me personally, I called rand.Seed manually beforehand and simply had no change in behaviour. But for people expecting something deterministic, this might be confusing. Additionaly, this library might want to stop using globalrand and instead allow specifying a source, as this makes it easier to use within projects that use randomness for other purposes.

Bios-Marcel added a commit to Bios-Marcel/golang-petname that referenced this issue Aug 26, 2023
Pre go1.20, rand.Seed needed to be called for randomness. Meaning, that
math/rand delivered deterministic values by default. This was changed
however. To restore the old behaviour, we need to manually seed our
random generator with a constant.

Additionally, this introduce a local instance of rand.Rand, in order
to prevent unexpected behaviour with other code that relies on the
global math/rand package.

Fixes dustinkirkland#10 (The readme and docs explain this, therefore I consider this
solved, as it is by design and not a bug)

Fixes dustinkirkland#15
Bios-Marcel added a commit to Bios-Marcel/golang-petname that referenced this issue Aug 26, 2023
Pre go1.20, rand.Seed needed to be called for randomness. Meaning, that
math/rand delivered deterministic values by default. This was changed
however. To restore the old behaviour, we need to manually seed our
random generator with a constant.

Additionally, this introduce a local instance of rand.Rand, in order
to prevent unexpected behaviour with other code that relies on the
global math/rand package.

Fixes dustinkirkland#10 (The readme and docs explain this, therefore I consider this
solved, as it is by design and not a bug)

Fixes dustinkirkland#15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant