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
B- The backend type that implementsRandomBackend
§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>
impl<B: RandomBackend> Rng<B>
Sourcepub fn new(backend: B) -> Self
pub fn new(backend: B) -> Self
Examples found in repository?
More examples
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}Sourcepub fn next_u64(&mut self) -> u64
pub fn next_u64(&mut self) -> u64
Examples found in repository?
More examples
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}Sourcepub fn next_f64(&mut self) -> f64
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)
Sourcepub fn next_f32(&mut self) -> f32
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.
Sourcepub fn gen_range(&mut self, min: u64, max: u64) -> Result<u64, AporiaError>
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 boundmax- 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.
Sourcepub fn gen_range_f64(&mut self, min: f64, max: f64) -> Result<f64, AporiaError>
pub fn gen_range_f64(&mut self, min: f64, max: f64) -> Result<f64, AporiaError>
Sourcepub fn fill_bytes(&mut self, buf: &mut [u8])
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