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: Example should demonstrate (in comments) how to do proper seeding #25325

Closed
kevinburke opened this issue May 10, 2018 · 3 comments
Closed

Comments

@kevinburke
Copy link
Contributor

@kevinburke kevinburke commented May 10, 2018

The example for math/rand reads:

rand.Seed(42) // Try changing this number!
answers := []string{
    "It is certain",
    "It is decidedly so",
    // ...
}
fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])

"Try changing this number" isn't super helpful advice (change to what?) and I have to look up what to plug in every time. For example last time I needed a math/rand, I copied from the other example, but I created my own *rand.Source when I didn't need to, because that's what the other example does.

I have been using Go for 5 years now and if I have to look up how to do it, odds are that other people do too.

It would be good if we could provide a better value in the comment, for example:

// "rand.Seed(42)" produces predictable outputs each time. To produce random 
// outputs on each run, replace with rand.Seed(time.Now().UnixNano())
rand.Seed(42) 
answers := []string{
    "It is certain",
    "It is decidedly so",
    // ...
}
fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])
@agnivade
Copy link
Contributor

@agnivade agnivade commented May 10, 2018

/cc @robpike - thoughts ?

@robpike
Copy link
Contributor

@robpike robpike commented May 10, 2018

It's not clear to me how much of the computing basics the documentation for Go must provide. To me, the need to explain seeding seems below the bar. I do admit though that "chang[e] this number" is poor. What I'm not sure of is what the right seed should be. I believe UnixNano can be very poor on some systems, but using crypto/rand to seed is overkill. Rather than just giving a recipe, perhaps explain:

Seeding with a constant results in the same random sequence each run. For different numbers, seed with an unpredictable value, such as time.Now().UnixNano(), which yields a large constantly-changing number. Or use crypto/rand to seed for even less predictability.

@agnivade agnivade added NeedsFix and removed NeedsDecision labels May 11, 2018
@gopherbot
Copy link

@gopherbot gopherbot commented May 11, 2018

Change https://golang.org/cl/112775 mentions this issue: math/rand: clarify documentation for Seed example

@gopherbot gopherbot closed this in f94b5a8 May 12, 2018
@golang golang locked and limited conversation to collaborators May 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.