From f8a9211740561b1e177e5e36c1b1a6ce923492e8 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 18 Aug 2014 15:38:47 -0400 Subject: [PATCH 1/2] Explain modulo in the guide. Fixes #15954 --- src/doc/guide.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index 2593ddc556b..194205c561e 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -1888,8 +1888,15 @@ fn main() { The first thing we changed was to `use std::rand`, as the docs explained. We then added in a `let` expression to create a variable binding -named `secret_number`, and we printed out its result. Let's try to compile -this using `cargo build`: +named `secret_number`, and we printed out its result. + +Also, you may wonder why we are using `%` on the result of `rand::random()`. +This operator is called 'modulo', and it returns the remainder of a division. +By taking the modulo of the result of `rand::random()`, we're limiting the +values to be between 0 and 99. Then, we add one to the result, making it from 1 +to 100. + +Let's try to compile this using `cargo build`: ```{notrust,no_run} $ cargo build From c88feffde4f5043adf07a6837026f228e20b67e6 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 18 Aug 2014 15:50:42 -0400 Subject: [PATCH 2/2] Make comment about small bias in %. Fixes #16354. --- src/doc/guide.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index 194205c561e..f6fa57fe143 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -1894,7 +1894,8 @@ Also, you may wonder why we are using `%` on the result of `rand::random()`. This operator is called 'modulo', and it returns the remainder of a division. By taking the modulo of the result of `rand::random()`, we're limiting the values to be between 0 and 99. Then, we add one to the result, making it from 1 -to 100. +to 100. Using modulo can give you a very, very small bias in the result, but +for this example, it is not important. Let's try to compile this using `cargo build`: