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 RandomSource {
fn fill_bytes(&mut self, bytes: &mut [u8]);
}
pub trait Distribution<T> {
fn sample(&self, source: &mut (impl RandomSource + ?Sized)) -> T;
}
impl Distribution<bool> for RangeFull { ... }
impl Distribution</* all integer types */> for /* all range types */ { ... }
// std::random (additionally)
pub struct DefaultRandomSource;
impl RandomSource for DefaultRandomSource { ... }
pub fn random<T>(dist: &(impl Distribution<T> + ?Sized)) -> 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>and newrandomUnresolved 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 ↩