Skip to content

Commit 887efe1

Browse files
committed
SmallRng: Improve documentation
Now initializing using `NewRng` is used to simplify the examples. The documentation for `SmallRng` recommends using `thread_rng` when initializing a lot of generators.
1 parent 075593f commit 887efe1

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/distributions/exponential.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ use distributions::{ziggurat, ziggurat_tables, Distribution};
3030
///
3131
/// # Example
3232
/// ```rust
33-
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
33+
/// use rand::{NewRng, SmallRng, Rng};
3434
/// use rand::distributions::Exp1;
3535
///
36-
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
37-
/// .unwrap().sample(Exp1);
36+
/// let val: f64 = SmallRng::new().unwrap().sample(Exp1);
3837
/// println!("{}", val);
3938
/// ```
4039
#[derive(Clone, Copy, Debug)]

src/distributions/float.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ macro_rules! float_impls {
4949
///
5050
/// # Example
5151
/// ```rust
52-
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
52+
/// use rand::{NewRng, SmallRng, Rng};
5353
/// use rand::distributions::Uniform;
5454
///
55-
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
56-
/// .unwrap().sample(Uniform);
55+
/// let val: f32 = SmallRng::new().unwrap().sample(Uniform);
5756
/// println!("f32 from (0,1): {}", val);
5857
/// ```
5958
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {

src/distributions/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,10 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
163163
///
164164
/// # Example
165165
/// ```rust
166-
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
166+
/// use rand::{NewRng, SmallRng, Rng};
167167
/// use rand::distributions::Uniform;
168168
///
169-
/// let val: f32 = SmallRng::from_rng(&mut thread_rng())
170-
/// .unwrap().sample(Uniform);
169+
/// let val: f32 = SmallRng::new().unwrap().sample(Uniform);
171170
/// println!("f32 from [0,1): {}", val);
172171
/// ```
173172
///

src/distributions/normal.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ use distributions::{ziggurat, ziggurat_tables, Distribution, Uniform};
2828
///
2929
/// # Example
3030
/// ```rust
31-
/// use rand::{SeedableRng, SmallRng, Rng, thread_rng};
31+
/// use rand::{NewRng, SmallRng, Rng};
3232
/// use rand::distributions::StandardNormal;
3333
///
34-
/// let val: f64 = SmallRng::from_rng(&mut thread_rng())
35-
/// .unwrap().sample(StandardNormal);
34+
/// let val: f64 = SmallRng::new().unwrap().sample(StandardNormal);
3635
/// println!("{}", val);
3736
/// ```
3837
#[derive(Clone, Copy, Debug)]

src/lib.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,8 +1088,8 @@ impl SeedableRng for StdRng {
10881088

10891089
/// An RNG recommended when small state, cheap initialization and good
10901090
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
1091-
/// efficient on the current platform, without consideration for cryptography or
1092-
/// security. The size of its state is much smaller than for `StdRng`.
1091+
/// efficient on the current platform, **without consideration for cryptography
1092+
/// or security**. The size of its state is much smaller than for `StdRng`.
10931093
///
10941094
/// Reproducibility of output from this generator is however not required, thus
10951095
/// future library versions may use a different internal generator with
@@ -1099,10 +1099,30 @@ impl SeedableRng for StdRng {
10991099
///
11001100
/// The current algorithm used on all platforms is [Xorshift].
11011101
///
1102+
/// # Examples
1103+
///
1104+
/// Initializing `StdRng` with a random seed can be done using `NewRng`:
1105+
///
1106+
/// ```
1107+
/// use rand::{NewRng, SmallRng};
1108+
///
1109+
/// // Create small, cheap to initialize and fast RNG with a random seed.
1110+
/// // The randomness is supplied by the operating system.
1111+
/// let mut small_rng = SmallRng::new().unwrap();
1112+
/// ```
1113+
///
1114+
/// When initializing a lot of `SmallRng`, using `thread_rng` can be more
1115+
/// efficient:
1116+
///
11021117
/// ```
11031118
/// use rand::{SeedableRng, SmallRng, thread_rng};
11041119
///
1105-
/// let _rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
1120+
/// // Create a big, expensive to initialize and slower, but unpredictable RNG.
1121+
/// // This is cached and done only once per thread.
1122+
/// let mut thread_rng = thread_rng();
1123+
/// // Create small, cheap to initialize and fast RNG with a random seed.
1124+
/// // This is very unlikely to fail.
1125+
/// let mut small_rng = SmallRng::from_rng(&mut thread_rng).unwrap();
11061126
/// ```
11071127
///
11081128
/// [Xorshift]: struct.XorShiftRng.html

0 commit comments

Comments
 (0)