pub struct ReadRng<R> { /* fields omitted */ }
An RNG that reads random bytes straight from any type supporting
[std::io::Read
], for example files.
This will work best with an infinite reader, but that is not required.
This can be used with /dev/urandom
on Unix but it is recommended to use
OsRng
instead.
ReadRng
uses [std::io::Read::read_exact
], which retries on interrupts.
All other errors from the underlying reader, including when it does not
have enough data, will only be reported through try_fill_bytes
.
The other [RngCore
] methods will panic in case of an error.
use rand::Rng;
use rand::rngs::adapter::ReadRng;
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
let mut rng = ReadRng::new(&data[..]);
println!("{:x}", rng.gen::<u32>());
Create a new ReadRng
from a Read
.
Formats the value using the given formatter. Read more
Fill dest
entirely with random data. Read more
Return a random value supporting the [Standard
] distribution. Read more
Generate a random value in the range [low
, high
), i.e. inclusive of low
and exclusive of high
. Read more
Sample a new value, using the given distribution. Read more
Important traits for DistIter<'a, D, R, T>
impl<'a, D, R, T> Iterator for DistIter<'a, D, R, T> where
D: Distribution<T>,
R: Rng + 'a, type Item = T;
Create an iterator that generates values using the given distribution. Read more
Fill dest
entirely with random bytes (uniform value distribution), where dest
is any type supporting [AsByteSliceMut
], namely slices and arrays over primitive integer types (i8
, i16
, u32
, etc.). Read more
Fill dest
entirely with random bytes (uniform value distribution), where dest
is any type supporting [AsByteSliceMut
], namely slices and arrays over primitive integer types (i8
, i16
, u32
, etc.). Read more
Return a bool with a probability p
of being true. Read more
Return a bool with a probability of numerator/denominator
of being true. I.e. gen_ratio(2, 3)
has chance of 2 in 3, or about 67%, of returning true. If numerator == denominator
, then the returned value is guaranteed to be true
. If numerator == 0
, then the returned value is guaranteed to be false
. Read more
Deprecated since 0.6.0
: use SliceRandom::choose instead
Return a random element from values
. Read more
Deprecated since 0.6.0
: use SliceRandom::choose_mut instead
Return a mutable pointer to a random element from values
. Read more
Deprecated since 0.6.0
: use SliceRandom::shuffle instead
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more