Skip to content

Commit de83b90

Browse files
committed
Some polish
1 parent 237916f commit de83b90

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

src/lib.rs

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
//! To quickly get you started, the easiest and most high-level way to just get
1414
//! some random value is to use [`random()`].
1515
//!
16-
//! ```rust
16+
//! ```
1717
//! let x: u8 = rand::random();
1818
//! println!("{}", x);
1919
//!
2020
//! let y = rand::random::<f64>();
2121
//! println!("{}", y);
2222
//!
2323
//! if rand::random() { // generates a boolean
24-
//! println!("Better lucky than good!");
24+
//! println!("Heads!");
2525
//! }
2626
//! ```
2727
//!
2828
//! This functionality is however very basic. As soon as you need something
2929
//! slightly more specific, like a random value in some range, you have to know
30-
//! a bit more about how things work, which are discussed below.
30+
//! a bit more about how things work, which will be discussed below.
3131
//!
3232
//! ## Examples
3333
//!
@@ -45,19 +45,21 @@
4545
//!
4646
//! - a random number generator (RNG);
4747
//! - some function to transform the 'bag of bits' from the RNG to a value you
48-
//! can use.
48+
//! can use (which we call a distribution, although we use the term for a bit
49+
//! more functionality than what fits the meaning).
4950
//!
5051
//! There are many kinds of RNGs, with different trade-offs. Rand comes with a
51-
//! default, [`thread_rng`]. The numbers it generates are suitable to use in all
52-
//! situations.
52+
//! good all-use default, [`thread_rng`]. It is reasonably fast and has good
53+
//! quality (secure and without patterns).
5354
//!
5455
//! To turn the output of the RNG into something usable, you usually want to use
5556
//! the methods from the [`Rng`] trait:
5657
//!
57-
//! - [`gen`] generates a random value that fits the type.
58-
//! For example `let val: u16 = rng.gen()` will just fill an `u16` with 16
59-
//! random bits. But for floats the value will be generated between 0 and 1;
60-
//! which is more usable than any value between 0 and infinity.
58+
//! - [`gen`] generates a random value appropriate for the type.
59+
//! For example `let val: u16 = rng.gen()` will produces a random value
60+
//! between 0 and `std::u16::MAX`. But for floats the value will be generated
61+
//! uniformly between 0 and 1, which is more useful than a completely random
62+
//! float value (including NaN and infinity);
6163
//! - [`gen_range`] samples from a specific range of values;
6264
//! - [`sample`], for all situations where you need something slightly more
6365
//! complex.
@@ -66,7 +68,7 @@
6668
//!
6769
//! ### Examples
6870
//!
69-
//! ```rust
71+
//! ```
7072
//! // Rng is the main trait and needs to be imported:
7173
//! use rand::{Rng, thread_rng};
7274
//!
@@ -88,30 +90,38 @@
8890
//! impossible, but hard and slow. Generally the operating systems collects some
8991
//! of them, by measuring different kinds of noice/jitter in the hardware.
9092
//!
91-
//! What is always used instead is a psuedo-random number generator. This is a
92-
//! clever algorithm that can produce an infinite stream of random numbers from
93-
//! a small seed. There exist algorithms that produce random numbers that are in
94-
//! no measurable way any less random than pulling values 'out of thin air'.
93+
//! What is always used instead is a psuedo-random number generator (PRNG). This
94+
//! is a clever algorithm that can produce an infinite stream of psuedo-random
95+
//! numbers from a small seed. There exist algorithms that produce random
96+
//! numbers that are in no practically measurable way any less random than
97+
//! pulling values 'out of thin air'. Except that the random number stream can
98+
//! be reproduced if you know the seed.
9599
//!
96100
//! There are two very different groups interested in developing PRNGs: one
97-
//! interested in simulations and statistics, another in cryptography. From both
98-
//! groups Rand picks one good PRNG algorithm. They are exposed as:
101+
//! interested in simulations and statistics, another in cryptography (sometimes
102+
//! PRNGs from the latter are more specifically called a CSPRNG). From both
103+
//! groups Rand picks one good algorithm. They are exposed as:
99104
//!
100105
//! - [`SmallRng`]. Uses little memory, and is very fast.
101106
//! Note: The currently picked algorithm is not ideal, but in a future version
102107
//! the values will for all practical purposes be statistically indiscernible
103108
//! from true randomness.
104-
//! - [`StdRng`]. In addition to the output being indiscernible from true
105-
//! randomness, this PRNG is unpredictable, even if you know the
106-
//! implementation. This requirement makes it slower and/or use more memory
107-
//! however.
109+
//! - [`StdRng`]. It is a requirement for a CSPRNG that the output is basically
110+
//! indiscernible from true randomness, as otherwise information could be
111+
//! leaked. In addition this PRNG is unpredictable, even if you know the
112+
//! implementation. This is an important property for any situation where
113+
//! there might be an adversary, like cryptography, but certainly not limited
114+
//! to that. This requirement makes it slower and/or use more memory however.
108115
//!
109116
//! One of the problems with PRNGs is that they need a seed (which is what the
110117
//! [`SeedableRng`] trait is for). Where do you get such a seed? Two options:
111118
//!
112119
//! - Just use a constant. This is good when you need reproducible results, for
113120
//! example to procedurally generate a game world). See the
114-
//! [`SeedableRng::from_seed`] documentation.
121+
//! [`SeedableRng::from_seed`] documentation. You may want to combine this
122+
//! with a named PRNG from the [`prng`] module or from other crates, because
123+
//! the algorithm of [`StdRng`] and [`SmallRng`] can change per release and/or
124+
//! platform.
115125
//! - Get a random value from somewhere at runtime. The
116126
//! [`FromEntropy::from_entropy`] method helps here. It will usually get the
117127
//! seed from the operating system's PRNG (using [`EntropyRng`]).
@@ -123,27 +133,26 @@
123133
//!
124134
//! [`ThreadRng`] abstracts away most of these issues. It uses [`StdRng`], which
125135
//! is always a safe choice. Seeding happens automatically on first use. The
126-
//! PRNG its state is stored in thread local storage, so no need to plumb the
136+
//! PRNG's state is stored in thread local storage, so no need to plumb the
127137
//! state through an API. You can easily get a mutable reference to it with the
128138
//! [`thread_rng`] function.
129139
//!
130140
//! ### Conclusion
131141
//!
132142
//! - [`thread_rng`] is what you often want to use.
133-
//! - If you want more control or better performance, use [`StdRng`] or
134-
//! [`SmallRng`].
143+
//! - If you want more control, flexibility, or better performance, use
144+
//! [`StdRng`], [`SmallRng`] or choose a specific PRNG algorithm.
135145
//! - If you do not use [`thread_rng`], use [`FromEntropy::from_entropy`] for
136146
//! seeding.
137147
//! - If you need reproducibility, use [`SeedableRng::from_seed`] combined with
138-
//! a named PRNG from the [`prng`] module or other crates (i.e. not
139-
//! [`StdRng`]/[`SmallRng`] which may be adjusted in the future.)
148+
//! a named PRNG.
140149
//!
141150
//! For more information, and notes on cryptographic security, see the
142151
//! documentation in the [`prng`] module.
143152
//!
144153
//! ### Examples
145154
//!
146-
//! Examples of seeding PRNGs.
155+
//! Examples of seeding PRNGs:
147156
//!
148157
//! ```
149158
//! # use rand::{Rng, Error};
@@ -186,7 +195,7 @@
186195
//! generate floating point values in the (0, 1) interval.
187196
//!
188197
//! For floating point values you may want to use probability distributions.
189-
//! For example time of decay is often modelled with an exponential
198+
//! For example, time of decay is often modelled with an exponential
190199
//! distribution [`Exp`], and the log-normal, [`LogNormal`] distribution
191200
//! provides a good model of many natural phenomona.
192201
//!
@@ -203,7 +212,7 @@
203212
//!
204213
//! Sampling from a distribution:
205214
//!
206-
//! ```rust
215+
//! ```
207216
//! use rand::{thread_rng, Rng};
208217
//! use rand::distributions::Exp;
209218
//!
@@ -214,7 +223,7 @@
214223
//!
215224
//! Implementing the [`Standard`] distribution for a user type:
216225
//!
217-
//! ```rust
226+
//! ```
218227
//! # #![allow(dead_code)]
219228
//! use rand::Rng;
220229
//! use rand::distributions::{Distribution, Standard};
@@ -235,9 +244,11 @@
235244
//!
236245
//! Rand contains some more functionality not discussed above.
237246
//!
247+
//! - [`Rng::fill`] and [`Rng::try_fill`] as fast alternatives to fill a slice
248+
//! of integers.
238249
//! - [`Rng::shuffle`] can be used to shuffle slices.
239250
//! - [`Rng::choose`] to pick one element at random from a slice, and the
240-
//! functions in the [`seq`] the pick multiple.
251+
//! functions in the [`seq`] module to the pick multiple.
241252
//! - [`Rng::sample_iter`] to get an iterator for the choosen distribution.
242253
//! - [`Rng::gen_bool`] to generate events with weighted probability.
243254
//! - [`distributions::WeightedChoice`] can be used to pick elements at random
@@ -283,7 +294,7 @@
283294
//!
284295
//! ### Example
285296
//!
286-
//! ```rust
297+
//! ```
287298
//! # use rand::thread_rng;
288299
//! use rand::Rng;
289300
//!
@@ -308,6 +319,8 @@
308319
//! [`Open01`]: distributions/struct.Open01.html
309320
//! [`OsRng`]: os/struct.OsRng.html
310321
//! [`Rng::choose`]: trait.Rng.html#method.choose
322+
//! [`Rng::fill`]: trait.Rng.html#method.fill
323+
//! [`Rng::try_fill`]: trait.Rng.html#method.try_fill
311324
//! [`Rng::gen_bool`]: trait.Rng.html#method.gen_bool
312325
//! [`Rng::gen_range`]: trait.Rng.html#method.gen_range
313326
//! [`Rng::gen()`]: trait.Rng.html#method.gen
@@ -442,7 +455,7 @@ pub trait Rng: RngCore {
442455
///
443456
/// # Example
444457
///
445-
/// ```rust
458+
/// ```
446459
/// use rand::{thread_rng, Rng};
447460
///
448461
/// let mut rng = thread_rng();
@@ -471,7 +484,7 @@ pub trait Rng: RngCore {
471484
///
472485
/// # Example
473486
///
474-
/// ```rust
487+
/// ```
475488
/// use rand::{thread_rng, Rng};
476489
///
477490
/// let mut rng = thread_rng();
@@ -488,7 +501,7 @@ pub trait Rng: RngCore {
488501
///
489502
/// ### Example
490503
///
491-
/// ```rust
504+
/// ```
492505
/// use rand::{thread_rng, Rng};
493506
/// use rand::distributions::Uniform;
494507
///
@@ -503,7 +516,7 @@ pub trait Rng: RngCore {
503516
///
504517
/// # Example
505518
///
506-
/// ```rust
519+
/// ```
507520
/// use rand::{thread_rng, Rng};
508521
/// use rand::distributions::{Alphanumeric, Uniform, Standard};
509522
///
@@ -545,7 +558,7 @@ pub trait Rng: RngCore {
545558
///
546559
/// # Example
547560
///
548-
/// ```rust
561+
/// ```
549562
/// use rand::{thread_rng, Rng};
550563
///
551564
/// let mut arr = [0i8; 20];
@@ -574,7 +587,7 @@ pub trait Rng: RngCore {
574587
///
575588
/// # Example
576589
///
577-
/// ```rust
590+
/// ```
578591
/// # use rand::Error;
579592
/// use rand::{thread_rng, Rng};
580593
///
@@ -601,7 +614,7 @@ pub trait Rng: RngCore {
601614
///
602615
/// # Example
603616
///
604-
/// ```rust
617+
/// ```
605618
/// use rand::{thread_rng, Rng};
606619
///
607620
/// let mut rng = thread_rng();
@@ -666,7 +679,7 @@ pub trait Rng: RngCore {
666679
///
667680
/// # Example
668681
///
669-
/// ```rust
682+
/// ```
670683
/// use rand::{thread_rng, Rng};
671684
///
672685
/// let mut rng = thread_rng();
@@ -711,7 +724,7 @@ pub trait Rng: RngCore {
711724
///
712725
/// # Example
713726
///
714-
/// ```rust
727+
/// ```
715728
/// # #![allow(deprecated)]
716729
/// use rand::{thread_rng, Rng};
717730
///
@@ -734,7 +747,7 @@ pub trait Rng: RngCore {
734747
///
735748
/// # Example
736749
///
737-
/// ```rust
750+
/// ```
738751
/// # #![allow(deprecated)]
739752
/// use rand::{thread_rng, Rng};
740753
///
@@ -931,7 +944,7 @@ pub trait FromEntropy: SeedableRng {
931944
/// If all entropy sources fail this will panic. If you need to handle
932945
/// errors, use the following code, equivalent aside from error handling:
933946
///
934-
/// ```rust
947+
/// ```
935948
/// # use rand::Error;
936949
/// use rand::{Rng, StdRng, EntropyRng, SeedableRng};
937950
///
@@ -1118,7 +1131,7 @@ pub fn weak_rng() -> XorShiftRng {
11181131
///
11191132
/// # Example
11201133
///
1121-
/// ```rust
1134+
/// ```
11221135
/// # #![allow(deprecated)]
11231136
/// use rand::{thread_rng, sample};
11241137
///

src/thread_rng.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl CryptoRng for ThreadRng {}
128128
///
129129
/// # Examples
130130
///
131-
/// ```rust
131+
/// ```
132132
/// let x = rand::random::<u8>();
133133
/// println!("{}", x);
134134
///
@@ -143,7 +143,7 @@ impl CryptoRng for ThreadRng {}
143143
/// If you're calling `random()` in a loop, caching the generator as in the
144144
/// following example can increase performance.
145145
///
146-
/// ```rust
146+
/// ```
147147
/// # #![allow(deprecated)]
148148
/// use rand::Rng;
149149
///

0 commit comments

Comments
 (0)