-
-
Notifications
You must be signed in to change notification settings - Fork 489
Closed
Labels
X-bugType: bug reportType: bug report
Description
Summary
When I tried to use StepRng::new(1, 0) to test a special case of my crate, I find out the result of rng.random_range(0..=N) would always be 0.
After some experiments, I figure out the returned value has nothing to do with the initial value and the step of StepRng. The returned value is a constant, and it is the lower bound of the given range in random_range's parameter.
What behaviour is expected, and why?
The result of rng.random_range(0..=N) would return k for let mut rng = StepRng::new(k, 0) when k is in the range of 0..=N. Otherwise it is an UB.
Code sample
Here's a sample on the playground.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=db19fd77a6eb6195eecfcdfc5dce9f3b
use rand; // 0.9.0
use rand::Rng;
fn main() {
const N: u64 = 123;
const M1: u64 = 45;
const M2: u64 = 678;
const M3: u64 = 910;
const ULIMIT: u64 = 10 * N;
const STEP: u64 = 0;
let mut rng = rand::rngs::mock::StepRng::new(N, STEP);
let rnr1: u64 = rng.random_range(M1..=ULIMIT);
let rnr2: u64 = rng.random_range(M2..=ULIMIT);
let rnr3: u64 = rng.random_range(M3..=ULIMIT);
println!("step_rng.random_range({M1}..={ULIMIT}) = {rnr1}.");
println!("step_rng.random_range({M2}..={ULIMIT}) = {rnr2}.");
println!("step_rng.random_range({M3}..={ULIMIT}) = {rnr3}.");
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
X-bugType: bug reportType: bug report