Trait RandomBackend

Source
pub trait RandomBackend {
    // Required method
    fn next_u64(&mut self) -> u64;

    // Provided methods
    fn next_f64(&mut self) -> f64 { ... }
    fn next_u32(&mut self) -> u32 { ... }
    fn fill_bytes(&mut self, buf: &mut [u8]) { ... }
}
Expand description

Trait that defines the interface for random number generator backends.

This trait must be implemented by all RNG backends. It provides a minimal interface for generating random numbers, with a default implementation for floating-point numbers.

§Required Methods

  • next_u64: Generate the next 64-bit random integer

§Provided Methods

  • next_f64: Generate a random float in [0, 1) using next_u64

§Examples

use aporia::RandomBackend;

struct MyBackend {
    state: u64
}

impl RandomBackend for MyBackend {
    fn next_u64(&mut self) -> u64 {
        // Your implementation here
        self.state
    }
}

Required Methods§

Source

fn next_u64(&mut self) -> u64

Generates the next 64-bit unsigned integer.

This is the core method that must be implemented by all backends.

Provided Methods§

Source

fn next_f64(&mut self) -> f64

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

This method has a default implementation that converts the result of next_u64() to a floating-point number using the upper 53 bits.

Using the upper 53 bits ensures the value is in [0, 1) and matches the precision of f64 mantissa, avoiding the possibility of returning 1.0.

Source

fn next_u32(&mut self) -> u32

Generates the next 32-bit unsigned integer.

Default implementation returns the upper 32 bits of next_u64().

Source

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

Fills buf with random bytes using repeated next_u64() calls. The tail shorter than 8 bytes is handled with a final partial copy.

Implementors§