b8f9fa9b1b
Expand range sampling from the original u32-only helper to all primitive integer widths. The new gen_range_<int> methods cover 0..n sampling, while matching gen_range_<int>_in methods accept standard RangeBounds forms such as exclusive, inclusive, open-ended, and full-width ranges. Use rejection sampling against the matching primitive reader for each integer width. That keeps modulo bias out of the public helpers without introducing a general RNG trait or depending on rand. Full-width ranges fall back directly to the primitive reader because their span cannot be represented in the same integer type. Document the generated APIs with doctested examples and add a ranges example that demonstrates unsigned, signed, usize, and full-width sampling. Test Plan: - cargo test - cargo clippy - cargo clippy --benches - cargo clippy --tests - cargo +nightly fmt Refs: IDEAS.md ergonomics backlog
28 lines
819 B
Rust
28 lines
819 B
Rust
//! Run with: `cargo run --example demo`
|
|
|
|
use ez_urandom::{OsRandom, charset};
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
let mut rng = OsRandom::try_new()?;
|
|
|
|
println!("u8 : {}", rng.get_u8()?);
|
|
println!("u32 : {}", rng.get_u32()?);
|
|
println!("u64 : {}", rng.get_u64()?);
|
|
println!("i32 : {}", rng.get_i32()?);
|
|
|
|
println!("dice 1-6 : {}", rng.gen_range_u32(6)? + 1);
|
|
println!("offset : {}", rng.gen_range_i32_in(-10..=10)?);
|
|
println!("index : {}", rng.gen_range_usize_in(0..16)?);
|
|
|
|
let token = rng.string_from(charset::ALPHANUMERIC, 24)?;
|
|
println!("token : {token}");
|
|
|
|
let hex = rng.string_from(charset::HEX_LOWER, 32)?;
|
|
println!("hex : {hex}");
|
|
|
|
let pin = rng.string_from(charset::DIGITS, 6)?;
|
|
println!("pin : {pin}");
|
|
|
|
Ok(())
|
|
}
|