Skip to main content

RandomGenerator

Struct RandomGenerator 

Source
pub struct RandomGenerator { /* private fields */ }
Expand description

High-level random data generator with stateful RNG.

RandomGenerator provides convenient methods for generating various types of random data while maintaining an internal RNG state. It implements the RandomGeneratorTemplate trait for instance methods and provides static methods for one-time generation.

§Usage Patterns

§Stateful Generation

Create a generator once and reuse it for multiple operations:

use product_os_random::{RandomGenerator, RandomGeneratorTemplate};

let mut gen = RandomGenerator::new(None); // Uses default RNG
let bytes1 = gen.get_random_bytes(10);
let bytes2 = gen.get_random_bytes(10);
let number = gen.get_random_usize(1, 100);

§One-Time Generation

Use static methods for single random values without maintaining state:

use product_os_random::RandomGenerator;

let password = RandomGenerator::get_simple_random_password_one_time(16);
let alphanumeric = RandomGenerator::get_simple_random_alphanumeric_one_time(10);

§Feature-Gated Behavior

  • With core or send_only: Can create generator without providing RNG (uses StdRng::from_entropy())
  • With constrained only: Must provide an RNG, panics if None given

§Panics

Methods will panic in constrained mode if no RNG is provided. In core or send_only modes, a default RNG is used when None is provided.

Trait Implementations§

Source§

impl Clone for RandomGenerator

Source§

fn clone(&self) -> RandomGenerator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl RandomGeneratorTemplate for RandomGenerator

Available on crate feature rand only.
Source§

fn get_random_bytes(&mut self, len: usize) -> Vec<u8>

Generates a vector of random bytes. Read more
Source§

fn get_random_usize(&mut self, min: usize, max: usize) -> usize

Generates a random usize within the specified range (inclusive). Read more
Source§

fn get_random_u32(&mut self) -> u32

Generates a random u32. Read more
Source§

fn get_random_u64(&mut self) -> u64

Generates a random u64. Read more
Source§

fn get_random_string(&mut self, len: usize) -> String

Generates a random string of printable characters. Read more
Source§

fn get_random_from_characters(&mut self, len: usize, characters: &str) -> String

Generates a random string from the provided character set. Read more
Source§

fn get_random_alphanumeric(&mut self, len: usize) -> String

Generates a random alphanumeric string. Read more
Source§

fn generate_key(&mut self, len: usize) -> Vec<u8>

Generates a cryptographic key. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.