View all comments
Feature gate: #![feature(random)]
This is a tracking issue for secure random data generation support in std.
Central to this feature is the RandomSource trait inside core::random, which generates random bytes. std also exposes the platform's secure random number generator via the DefaultRandomSource type. There is a Distribution<T> trait for distributions that can sample random values of a specific type, and a random function for convenience to allow things like random(1..=6).
Public API
// core::random
pub trait Rng {
fn fill_bytes(&mut self, bytes: &mut [u8]);
}
impl<'a, R: Rng + ?Sized> Rng for &'a mut R { ... }
pub trait Distribution<T> {
fn sample(&self, source: &mut (impl Rng + ?Sized)) -> T;
}
impl Distribution<bool> for RangeFull { ... }
impl Distribution</* all integer types */> for RangeFull { ... }
impl Distribution</* all integer types */> for core::range::RangeInclusive<_> { ... }
// std::random (additionally)
pub struct SystemRng;
impl Rng for SystemRng { ... }
pub fn random<T>(dist: impl Distribution<T>) -> T;
Steps / History
Unresolved Questions
View all comments
Feature gate:
#![feature(random)]This is a tracking issue for secure random data generation support in
std.Central to this feature is the
RandomSourcetrait insidecore::random, which generates random bytes.stdalso exposes the platform's secure random number generator via theDefaultRandomSourcetype. There is aDistribution<T>trait for distributions that cansamplerandom values of a specific type, and arandomfunction for convenience to allow things likerandom(1..=6).Public API
Steps / History
randomfeature (alternative version) #129201Distribution<T>)RandomSource->Rng,DefaultRandomSource->SystemRng#157539Rngfor references #159435Rnga by-value parameter ofDistribution::sample#160205RngandSystemRng#157168Unresolved Questions
gen_bytesandDefaultRng, the implementation PR usesfill_bytesandDefaultRandomSource(see arguments progen_bytesand profill_bytes)Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩