Random

Struct Random 

Source
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

Source

pub fn get_entropy_status(&self) -> Result<EntropyStatus>

Get the current entropy status.

Source

pub fn bytes(&self, length: usize) -> Result<Vec<u8>>

Generate random bytes.

§Arguments
  • length - Number of bytes to generate
§Returns

A vector of random bytes.

Source

pub fn fill(&self, buffer: &mut [u8]) -> Result<()>

Fill a buffer with random bytes.

§Arguments
  • buffer - The buffer to fill
Source

pub fn next_u8(&self) -> Result<u8>

Generate a random u8.

Source

pub fn next_u16(&self) -> Result<u16>

Generate a random u16.

Source

pub fn next_u32(&self) -> Result<u32>

Generate a random u32.

Source

pub fn next_u64(&self) -> Result<u64>

Generate a random u64.

Source

pub fn next_i32(&self) -> Result<i32>

Generate a random i32.

Source

pub fn next_i64(&self) -> Result<i64>

Generate a random i64.

Source

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.

Source

pub fn next_u64_bound(&self, bound: u64) -> Result<u64>

Generate a random u64 in range [0, bound).

Source

pub fn randint(&self, min: i64, max: i64) -> Result<i64>

Generate a random integer in range [min, max].

Source

pub fn next_f32(&self) -> Result<f32>

Generate a random f32 in range [0.0, 1.0).

Source

pub fn next_f64(&self) -> Result<f64>

Generate a random f64 in range [0.0, 1.0).

Source

pub fn uniform(&self, min: f64, max: f64) -> Result<f64>

Generate a random f64 in range [min, max).

Source

pub fn next_bool(&self) -> Result<bool>

Generate a random boolean.

Source

pub fn next_bool_with_probability(&self, p: f64) -> Result<bool>

Generate a random boolean with given probability of being true.

Source

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.

Source

pub fn choice<'a, T>(&self, items: &'a [T]) -> Result<Option<&'a T>>

Select a random element from a slice.

Source

pub fn sample<T: Clone>(&self, items: &[T], n: usize) -> Result<Vec<T>>

Sample n unique elements from a slice without replacement.

Source

pub fn shuffle<T>(&self, items: &mut [T]) -> Result<()>

Shuffle a slice in place using Fisher-Yates algorithm.

Source

pub fn shuffled<T: Clone>(&self, items: &[T]) -> Result<Vec<T>>

Return a shuffled copy of a slice.

Trait Implementations§

Source§

impl Clone for Random

Source§

fn clone(&self) -> Random

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Random

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.