Overview
The RNG module provides cryptographically secure random number generation with multiple entropy sources. It automatically selects the best available randomness source and provides fallback mechanisms for maximum security across different platforms.
Key Features:
- Hardware-backed random number generation
- Multiple entropy sources with automatic fallback
- Cryptographically secure pseudorandom number generators
- Deterministic RNG for testing and reproducibility
- Secure key and nonce generation utilities
Secure RNG
The primary interface for generating cryptographically secure random numbers.
Basic Random Generation
Secure RNG Example
use forge_ec::rng::{SecureRng, RngCore};
fn secure_rng_example() -> Result<(), Box> {
// Initialize secure RNG (uses best available entropy source)
let mut rng = SecureRng::new()?;
// Generate random bytes
let mut random_bytes = [0u8; 32];
rng.fill_bytes(&mut random_bytes);
println!("Random bytes: {:?}", random_bytes);
// Generate random u64
let random_u64 = rng.next_u64();
println!("Random u64: {}", random_u64);
// Generate random in range
let random_in_range = rng.gen_range(1..=100);
println!("Random 1-100: {}", random_in_range);
Ok(())
}