Rng

Struct Rng 

Source
pub struct Rng<B: RandomBackend> { /* private fields */ }
Expand description

A random number generator that works with any backend implementing RandomBackend.

This struct provides a consistent interface for random number generation, regardless of the underlying algorithm used.

§Type Parameters

§Examples

use aporia::{Rng, backend::XorShift};

let backend = XorShift::new(12345);
let mut rng = Rng::new(backend);

let random_number = rng.next_u64();
let random_float = rng.next_f64();

Iterators and helpers:

use aporia::{Rng, backend::XorShift};
let mut rng = Rng::new(XorShift::new(1));
 
// Take 3 u64 values
let count = rng.iter_u64().take(3).count();
assert_eq!(count, 3);
 
// Use for-loop via IntoIterator for &mut Rng to consume a few values
let mut n = 0usize;
for _ in &mut rng {
    n += 1;
    if n == 4 { break; }
}
assert_eq!(n, 4);

Implementations§

Source§

impl<B: RandomBackend> Rng<B>

Source

pub fn new(backend: B) -> Self

Creates a new RNG with the specified backend.

§Arguments
  • backend - The RNG backend to use
Examples found in repository?
examples/lcg.rs (line 5)
3fn main() {
4    let backend = LCG::new(12345);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
More examples
Hide additional examples
examples/pcg.rs (line 5)
3fn main() {
4    let backend = PCG::new(12345, 1);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
examples/xorshift.rs (line 5)
3fn main() {
4    let backend = XorShift::new(12345);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
examples/total.rs (line 9)
6fn main() {
7    // Using PCG
8    let pcg = PCG::new(12345, 67890);
9    let mut rng1 = Rng::new(pcg);
10
11    // Using XorShift
12    let xorshift = XorShift::new(12345);
13    let mut rng2 = Rng::new(xorshift);
14
15    // Using LCG
16    let lcg = LCG::new(12345);
17    let mut rng3 = Rng::new(lcg);
18
19    // Using MT19937_64
20    let mt19937_64 = MT19937_64::new(12345);
21    let mut rng4 = Rng::new(mt19937_64);
22
23    // Using SplitMix64
24    let splitmix64 = SplitMix64::new(12345);
25    let mut rng5 = Rng::new(splitmix64);
26
27    // Using Xoshiro256StarStar
28    let xoshiro256starstar = Xoshiro256StarStar::new(12345);
29    let mut rng6 = Rng::new(xoshiro256starstar);
30
31    println!("PCG: {}", rng1.next_u64());
32    println!("XorShift: {}", rng2.next_u64());
33    println!("LCG: {}", rng3.next_u64());
34    println!("MT19937_64: {}", rng4.next_u64());
35    println!("SplitMix64: {}", rng5.next_u64());
36    println!("Xoshiro256StarStar: {}", rng6.next_u64());
37}
Source

pub fn next_u64(&mut self) -> u64

Generates the next 64-bit unsigned integer.

§Returns

A randomly generated u64 value

Examples found in repository?
examples/lcg.rs (line 6)
3fn main() {
4    let backend = LCG::new(12345);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
More examples
Hide additional examples
examples/pcg.rs (line 6)
3fn main() {
4    let backend = PCG::new(12345, 1);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
examples/xorshift.rs (line 6)
3fn main() {
4    let backend = XorShift::new(12345);
5    let mut rng = Rng::new(backend);
6    println!("Random number: {}", rng.next_u64());
7}
examples/total.rs (line 31)
6fn main() {
7    // Using PCG
8    let pcg = PCG::new(12345, 67890);
9    let mut rng1 = Rng::new(pcg);
10
11    // Using XorShift
12    let xorshift = XorShift::new(12345);
13    let mut rng2 = Rng::new(xorshift);
14
15    // Using LCG
16    let lcg = LCG::new(12345);
17    let mut rng3 = Rng::new(lcg);
18
19    // Using MT19937_64
20    let mt19937_64 = MT19937_64::new(12345);
21    let mut rng4 = Rng::new(mt19937_64);
22
23    // Using SplitMix64
24    let splitmix64 = SplitMix64::new(12345);
25    let mut rng5 = Rng::new(splitmix64);
26
27    // Using Xoshiro256StarStar
28    let xoshiro256starstar = Xoshiro256StarStar::new(12345);
29    let mut rng6 = Rng::new(xoshiro256starstar);
30
31    println!("PCG: {}", rng1.next_u64());
32    println!("XorShift: {}", rng2.next_u64());
33    println!("LCG: {}", rng3.next_u64());
34    println!("MT19937_64: {}", rng4.next_u64());
35    println!("SplitMix64: {}", rng5.next_u64());
36    println!("Xoshiro256StarStar: {}", rng6.next_u64());
37}
Source

pub fn next_f64(&mut self) -> f64

Generates the next floating-point number in the range [0, 1).

§Returns

A randomly generated f64 value between 0 (inclusive) and 1 (exclusive)

Source

pub fn next_u32(&mut self) -> u32

Generates the next 32-bit unsigned integer.

Source

pub fn next_f32(&mut self) -> f32

Generates the next 32-bit floating point number in [0, 1).

Uses the upper 24 bits of a u64 sample to match f32 mantissa width.

Source

pub fn next_bool(&mut self) -> bool

Generates a random boolean with p=0.5.

Source

pub fn gen_range(&mut self, min: u64, max: u64) -> Result<u64, AporiaError>

Generates a random number within the given range.

§Arguments
  • min - The inclusive lower bound
  • max - The exclusive upper bound
§Returns

A randomly generated number within the range [min, max)

§Panics

Panics if min >= max

§Notes

Uses the unbiased “zone” rejection method to avoid modulo bias. Let range = max - min. Compute zone = u64::MAX - (u64::MAX % range), which is the largest multiple of range that fits in a u64. Draw 64-bit values until v < zone, then return min + (v % range). Because zone is an exact multiple of range, the modulo is uniform.

Source

pub fn gen_range_f64(&mut self, min: f64, max: f64) -> Result<f64, AporiaError>

Generates a random floating-point number within the given range.

§Arguments
  • min - The inclusive lower bound
  • max - The exclusive upper bound
§Returns

A randomly generated f64 value within the range [min, max)

§Panics

Panics if min >= max

Source

pub fn fill_bytes(&mut self, buf: &mut [u8])

Fills buf with random bytes from the backend.

This is a convenience wrapper around RandomBackend::fill_bytes.

use aporia::{Rng, backend::XorShift};
let mut rng = Rng::new(XorShift::new(1));
let mut bytes = [0u8; 16];
rng.fill_bytes(&mut bytes);
// `bytes` now contains pseudorandom data
Source§

impl<B: RandomBackend> Rng<B>

Source

pub fn iter_u64(&mut self) -> U64Iter<'_, B>

Returns an iterator that yields u64 values indefinitely.

Source

pub fn iter_f64(&mut self) -> F64Iter<'_, B>

Returns an iterator that yields f64 values in [0, 1) indefinitely.

Trait Implementations§

Source§

impl<B> Clone for Rng<B>
where B: RandomBackend + Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<B> Debug for Rng<B>
where B: RandomBackend + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a, B: RandomBackend> IntoIterator for &'a mut Rng<B>

Source§

type Item = u64

The type of the elements being iterated over.
Source§

type IntoIter = U64Iter<'a, B>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<B> Freeze for Rng<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for Rng<B>
where B: RefUnwindSafe,

§

impl<B> Send for Rng<B>
where B: Send,

§

impl<B> Sync for Rng<B>
where B: Sync,

§

impl<B> Unpin for Rng<B>
where B: Unpin,

§

impl<B> UnwindSafe for Rng<B>
where B: UnwindSafe,

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.