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

math/rand: Add auto-seed info to deprecation notice #59331

Closed
natefinch opened this issue Mar 30, 2023 · 1 comment
Closed

math/rand: Add auto-seed info to deprecation notice #59331

natefinch opened this issue Mar 30, 2023 · 1 comment
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.

Comments

@natefinch
Copy link
Contributor

The current deprecation notice on rand.Seed only addresses what I think is a tiny corner case for why most applications call rand.Seed.

Deprecated: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access.

The thing is, since the beginning of time, many applications have been calling rand.Seed on startup because otherwise math/rand got seeded with a static value. So they actually wanted the opposite of what this deprecation notice says. Which makes this deprecation notice very confusing.

I know that there is a sentence above the notice in the docs that states that the seed is now set randomly at program startup, but many automated tools simply extract the deprecation notice to display to the developer, and the existing notice only applies to what I expect is about 0.1% of usage, and says nothing about the 99.9% case, where a dev was doing the (previously, mostly) right thing, and calling something like rand.Seed(time.Now().UnixNano()).

One more short sentence at the beginning of the deprecation notice would save a lot of confusion. Something like this:

Deprecated: The generator is now seeded randomly at program startup. Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access.

Personally, I think the existing deprecation notice is too long and editorializes too much about what could go wrong. I'd skip right to the important bits:

Deprecated: The generator is now always seeded randomly at program startup. Programs that need a specific sequence of results should use New(NewSource(seed)) to obtain a random generator.

(also note that the current documentation has a typo in the deprecation notice where it says NewRand( but should say New()

What version of Go are you using (go version)?

1.20

Does this issue reproduce with the latest release?

yes

@seankhliao seankhliao changed the title math/Rand: Add auto-seed info to deprecation notice math/rand: Add auto-seed info to deprecation notice Mar 30, 2023
@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Mar 30, 2023
@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Mar 31, 2023
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 31, 2023
@gopherbot
Copy link

Change https://go.dev/cl/481915 mentions this issue: math/rand: clarify Seed deprecation note

@golang golang locked and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants