Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate_range panics if the upper bound is 0 #24

Closed
tesselode opened this issue Jan 31, 2021 · 5 comments
Closed

generate_range panics if the upper bound is 0 #24

tesselode opened this issue Jan 31, 2021 · 5 comments
Milestone

Comments

@tesselode
Copy link

Example:

use nanorand::{tls_rng, RNG};

fn main() {
    println!("{}", tls_rng().generate_range::<usize>(3, 0));
}

Produces this output:

thread 'main' panicked at 'attempt to calculate the remainder with a divisor of zero', C:\Users\Andrew\.cargo\registry\src\github.com-1ecc6299db9ec823\nanorand-0.5.2\src\gen.rs:51:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@Absolucy
Copy link
Owner

I mean, there's an assert anyways: https://github.com/aspenluxxxy/nanorand-rs/blob/7e1704e6a80697b7b4bda4d93a8a476748379463/nanorand/src/gen.rs#L49-L51

and there's currently no support for generating signed integers anyways, so this is expected behavior? where are your running into this?

@tesselode
Copy link
Author

I ran into it on accident because I was trying to pick a random index of a Vec with length zero. It's fair that this would cause a panic, but the error message I got didn't point me towards the problem.

tesselode added a commit to tesselode/kira that referenced this issue Feb 14, 2021
nanorand has some bugs and weirdness that make me not fully trust it

Absolucy/nanorand-rs#24

Absolucy/nanorand-rs#21

for a fundamental thing like randomness i'd rather use a battle-tested lib
@Absolucy Absolucy added this to the 0.6 milestone Jul 7, 2021
@Absolucy
Copy link
Owner

Absolucy commented Jul 7, 2021

it should no longer panic.

@Absolucy Absolucy closed this as completed Jul 7, 2021
@matklad
Copy link

matklad commented Jul 8, 2021

If I am reading the code correctly, it’ll now return the lower bound for an empty range. I wonder if that’s the desired behavior?

Looking at the prior art, rand, fastrand and Python’s random all panic on empty ranges. At a first glance, panicking does make sense, and violating range.contains(&result) doesn’t.

Perhaps just the panic error message needs to be adjusted? It seems that (guessing here, the report isn’t supper clear) this is what tesselode is asking for, it’s not “make it non panic by returning something arbitrary”.

@tesselode
Copy link
Author

Yeah, looking back, this was not a good issue report. An invalid range should cause a panic, it should just have a proper error message. I think I was confused about whether this should panic or not because the error message looked like it was caused by an internal bug, not user error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants