-
Notifications
You must be signed in to change notification settings - Fork 12.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
guide: random secret number remarks #16354
Comments
The first is true, but we never talked about signed vs. unsigned, so I just left it at that, rather than dig into those details. You can't understand why rand::random can't infer the type without understanding generics, which we don't get to till much later. The last one is true, but is related to #15526 . until that's resolved, all numbers are suffixed, basically. I haven't actually determined a good guideline for what should be suffixed and what shouldn't, with regards to style. |
One shouldn't encourage generating random integers like that, this way the they're not equally distributed. The right way should be:
|
cc #15954 @tbu- that also requires importing the |
@huonw I think that showing wrong examples is bad. I'd mention it as "The |
I think it should at least be mentioned that this approach is not ideal. |
|
Encouraging incorrect code is still not a great thing to do anywhere. It also means people who are aware of the issue are going to put less faith in the guide once they spot it. |
If |
The |
Why? We don't need to care that it's not perfectly uniform. |
Phrased another way, if random isn't acceptable here, when IS it acceptable? |
It is "acceptable" when you want to generate a random number without restrictions. For I agree that in this use case the minimal bias does not matter. |
It might be worth mentioning that generating a
uint
directly is more efficient than generating anint
and callingabs
(unless the compiler is very clever).When reading the code I was wondering why
would not compile. Shouldn't the compiler be able to infer the correct specialization of
random
?Also, it might be more idiomatic to write
than using redundant integer literals
The text was updated successfully, but these errors were encountered: