Trait rand::NewRng [] [src]

pub trait NewRng: SeedableRng {
    fn new() -> Self;
}

A convenient way to seed new algorithmic generators with fresh entropy from EntropyRng.

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.

Example

use rand::{StdRng, Rng, NewRng};

let mut rng = StdRng::new();
println!("Random die roll: {}", rng.gen_range(1, 7));

Required Methods

Creates a new instance, automatically seeded with fresh entropy.

Normally this will use OsRng, but if that fails JitterRng will be used instead. Both should be suitable for cryptography. It is possible that both entropy sources will fail though unlikely; failures would almost certainly be platform limitations or build issues, i.e. most applications targetting PC/mobile platforms should not need to worry about this failing.

If all entropy sources fail this will panic. If you need to handle errors, use the following code, equivalent aside from error handling:

use rand::{Rng, StdRng, EntropyRng, SeedableRng, Error};
 
fn foo() -> Result<(), Error> {
    // This uses StdRng, but is valid for any R: SeedableRng
    let mut rng = StdRng::from_rng(EntropyRng::new())?;
     
    println!("random number: {}", rng.gen_range(1, 10));
    Ok(())
}

Implementors