feat: add unbiased integer range helpers
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
This commit is contained in:
@@ -11,6 +11,8 @@ fn main() -> std::io::Result<()> {
|
||||
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}");
|
||||
|
||||
Reference in New Issue
Block a user