Skip to content

rust-guide::13.3 Generating a secret number #15954

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

Closed
anmej opened this issue Jul 24, 2014 · 5 comments · Fixed by #16585
Closed

rust-guide::13.3 Generating a secret number #15954

anmej opened this issue Jul 24, 2014 · 5 comments · Fixed by #16585

Comments

@anmej
Copy link

anmej commented Jul 24, 2014

cc @steveklabnik

This part is somewhat confusing:
let secret_number = (rand::random::<int>() % 100i) + 1i;

My suggestion is: either mention that rand::random::<int>() samples the whole range of int and you use modulo to narrow the range. Or use the std::rand::distributions::Range function. This way you also don't get modulo bias.

@sinistersnare
Copy link
Contributor

cc @steveklabnik

(If you want to mention someone, use @<THEIR_USERNAME>)

@anmej
Copy link
Author

anmej commented Jul 24, 2014

Done, thanks.

@huonw
Copy link
Member

huonw commented Jul 24, 2014

Or use the 'std::rand::distributions::Range' function. This way you also don't get modulo bias.

I pulled this up in the original review: #15729 (comment). As you can see the modulo bias is miniscule (even on 32-bit platforms it is only billionths).

@anmej
Copy link
Author

anmej commented Jul 25, 2014

The introduction of modulo bias is not the main issue. The issue is that I, as a newbie, got confused by the %100 trick, so maybe it needs some more explanation.

@treeman
Copy link
Contributor

treeman commented Jul 26, 2014

I agree. std::rand::distributions::Range should also be the recommended way to generate a random number in a range. If it will be expanded on later in the guide, that's fine, but then some more explanation here could be nice.

@anmej If you surround your code with backticks, like let a = 2; then you'll get code sytax let a = 2;

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.

4 participants