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

High limit on gen_range is never returned #167

Closed
mightypenguin opened this issue Aug 24, 2017 · 3 comments
Closed

High limit on gen_range is never returned #167

mightypenguin opened this issue Aug 24, 2017 · 3 comments

Comments

@mightypenguin
Copy link

mightypenguin commented Aug 24, 2017

Going through the "guess_number" rust program example and using this line:

let secret_number = rand::thread_rng().gen_range(0, 1);

secret_number will never have a value of 1 (at least not during repeated testing)
Also true for slightly larger range like (0, 5) that the high limit is not returned.

It's probably ok if the high limit is never returned, but the documentation should indicate that the high limit is not inclusive (while the low limit IS inclusive). I think it's closer to user expectations to make them both inclusive. The current behavior does appear to coincide with Python's but is non-intuitive.

Rust 1.19.0
RHEL7 in a VM

@burdges
Copy link
Contributor

burdges commented Aug 24, 2017

Rust's ranges exclude the right hand bound normally. You must ask for an inclusive range explicitly : https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html.

@pitdicker
Copy link
Contributor

I think this can be closed, as this is the intended behaviour and changing gen_range is a very subtle breaking change.

Note that when the rand crate revision lands it is possible to get inclusive ranges with distributions::Range::new_inclusive.

@dhardy
Copy link
Member

dhardy commented Dec 13, 2017

Ok, I'll leave this open as a reminder to check the documentation on gen_range. I'll be making a fairly big PR affecting this in the next few days.

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

4 participants