pub struct Random { /* private fields */ }Expand description
Random number generator subsystem.
Provides access to the hardware Quantum Random Number Generator (QRNG) for generating cryptographically secure random data.
§Example
use quantacore::{initialize, open_first_device};
initialize().unwrap();
let device = open_first_device().unwrap();
let random = device.random();
// Generate random bytes
let bytes = random.bytes(32).unwrap();
println!("Random: {}", hex::encode(&bytes));
// Generate random integer
let value = random.next_u64().unwrap();
// Generate random float in [0, 1)
let f = random.next_f64().unwrap();
// Check entropy status
let status = random.get_entropy_status().unwrap();
println!("Entropy level: {}%", status.level);Implementations§
Source§impl Random
impl Random
Sourcepub fn get_entropy_status(&self) -> Result<EntropyStatus>
pub fn get_entropy_status(&self) -> Result<EntropyStatus>
Get the current entropy status.
Sourcepub fn next_u32_bound(&self, bound: u32) -> Result<u32>
pub fn next_u32_bound(&self, bound: u32) -> Result<u32>
Generate a random u32 in range [0, bound).
Uses rejection sampling to ensure uniform distribution.
Sourcepub fn next_u64_bound(&self, bound: u64) -> Result<u64>
pub fn next_u64_bound(&self, bound: u64) -> Result<u64>
Generate a random u64 in range [0, bound).
Sourcepub fn randint(&self, min: i64, max: i64) -> Result<i64>
pub fn randint(&self, min: i64, max: i64) -> Result<i64>
Generate a random integer in range [min, max].
Sourcepub fn uniform(&self, min: f64, max: f64) -> Result<f64>
pub fn uniform(&self, min: f64, max: f64) -> Result<f64>
Generate a random f64 in range [min, max).
Sourcepub fn next_bool_with_probability(&self, p: f64) -> Result<bool>
pub fn next_bool_with_probability(&self, p: f64) -> Result<bool>
Generate a random boolean with given probability of being true.
Sourcepub fn uuid(&self) -> Result<String>
pub fn uuid(&self) -> Result<String>
Generate a UUID v4 string.
Returns a string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
where x is any hexadecimal digit and y is one of 8, 9, a, or b.
Sourcepub fn choice<'a, T>(&self, items: &'a [T]) -> Result<Option<&'a T>>
pub fn choice<'a, T>(&self, items: &'a [T]) -> Result<Option<&'a T>>
Select a random element from a slice.
Sourcepub fn sample<T: Clone>(&self, items: &[T], n: usize) -> Result<Vec<T>>
pub fn sample<T: Clone>(&self, items: &[T], n: usize) -> Result<Vec<T>>
Sample n unique elements from a slice without replacement.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Random
impl RefUnwindSafe for Random
impl Send for Random
impl Sync for Random
impl Unpin for Random
impl UnwindSafe for Random
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)