Skip to content

Commit

Permalink
Documentation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jan 20, 2018
1 parent 2f42698 commit 34239fa
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ use prng::Isaac64Rng as IsaacWordRng;

use distributions::{Range, IndependentSample};
use distributions::range::SampleRange;
#[cfg(feature="std")]use reseeding::{ReseedingRng, ReseedWithNew};
#[cfg(feature="std")] use reseeding::{ReseedingRng, ReseedWithNew};

// public modules
pub mod distributions;
Expand Down Expand Up @@ -736,12 +736,12 @@ impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> {
///
/// Each pseudo-random number generator (PRNG) should implement this.
pub trait SeedableRng: Sized {
/// Seed type, which is restricted to `u8` arrays with a length of
/// 4, 8, 12, 16, 24 and 32.
/// Seed type, which is restricted to types mutably-dereferencable as `u8`
/// arrays (we recommend `[u8; N]` for some `N`).
///
/// It is recommended to seed PRNG's with a seed of more than circa
/// 100 bits, which means an array of `[u8; 12]` or greater to avoid picking
/// RNG's with partially overlapping periods.
/// It is recommended to seed PRNGs with a seed of at least circa 100 bits,
/// which means an array of `[u8; 12]` or greater to avoid picking RNGs with
/// partially overlapping periods.
///
/// For cryptographic RNG's a seed of 256 bits is recommended, `[u8; 32]`.
type Seed: Sized + Default + AsMut<[u8]>;
Expand All @@ -764,28 +764,32 @@ pub trait SeedableRng: Sized {

/// Create a new PRNG seeded from another `Rng`.
///
/// This is the recommended way to initialize PRNGs. See the `NewRng`
/// trait that provides a convenient `new` method using `from_rng` and a
/// good entropy source.
/// This is the recommended way to initialize PRNGs. The [`NewRng`] trait
/// provides a convenient new method based on `from_rng`.
///
/// It is recommended to use a good source of randomness to initialize the
/// PRNG. Otherwise small PRNG's could show statistical bias in the first
/// couple of results, and possibly not use their entire period well.
/// Cryptographic PRNG's can be less secure or even insecure when they are
/// seeded from a non-cryptographic PRNG.
/// It is important to use a good source of randomness to initialize the
/// PRNG. Cryptographic PRNG may be rendered insecure when seeded from a
/// non-cryptographic PRNG or with insufficient entropy.
/// Many non-cryptographic PRNGs will show statistical bias in their first
/// results if their seed numbers are small or if there is a simple pattern
/// between them.
///
/// Examples of good RNG's for seeding are entropy sources like `OsRng` and
/// `JitterRng`. Also cryptographically secure PRNG's (like `thread_rng`)
/// can be used without hesitation.
/// Prefer to seed from a strong external entropy source like [`OsRng`] or
/// from a cryptographic PRNG; if creating a new generator for cryptography
/// you *must* do this.
///
/// Seeding a small PRNG from another small PRNG is be possible, but
/// Seeding a small PRNG from another small PRNG is possible, but
/// something to be careful with. An extreme example of how this can go
/// wrong is seeding an Xorshift RNG from another Xorshift RNG. That will
/// effectively clone the generator.
/// effectively clone the generator. In general seeding from a generator
/// which is hard to predict is probably okay.
///
/// PRNG implementations are allowed to assume that a good RNG is provided
/// for seeding, and that it is cryptographically secure when appropriate.
/// There are no reproducibility requirements like endianness conversion.
///
/// [`NewRng`]: trait.NewRng.html
/// [`OsRng`]: os/struct.OsRng.html
fn from_rng<R: Rng>(mut rng: R) -> Result<Self, Error> {
let mut seed = Self::Seed::default();
rng.try_fill_bytes(seed.as_mut())?;
Expand All @@ -794,13 +798,14 @@ pub trait SeedableRng: Sized {
}


/// Seeding mechanism for PRNGs, providing a `new` function.
/// This is the recommended way to create (pseudo) random number generators,
/// unless a deterministic seed is desired (in which case
/// `SeedableRng::from_seed` should be used).
/// A convenient way to seed new algorithmic generators, otherwise known as
/// pseudo-random number generators (PRNGs).
///
/// This is the recommended way to create PRNGs, unless a deterministic seed is
/// desired (in which case `SeedableRng::from_seed` should be used).
///
/// Note: this trait is automatically implemented for any PRNG implementing
/// `SeedableRng` and is not intended to be implemented by users.
/// [`SeedableRng`] and is not intended to be implemented by users.
///
/// ## Example
///
Expand All @@ -810,6 +815,8 @@ pub trait SeedableRng: Sized {
/// let mut rng = StdRng::new().unwrap();
/// println!("Random die roll: {}", rng.gen_range(1, 7));
/// ```
///
/// [`SeedableRng`]: trait.SeedableRng.html
#[cfg(feature="std")]
pub trait NewRng: SeedableRng {
/// Creates a new instance, automatically seeded with fresh entropy.
Expand Down

0 comments on commit 34239fa

Please sign in to comment.