-
-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
add rand::range
, random_bool, random_iter, random_ratio, fill as top-level helper functions
#1488
Conversation
src/lib.rs
Outdated
/// println!("{}", y); | ||
/// ``` | ||
/// | ||
/// If you're calling `range()` in a loop, caching the generator as in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean caching the distribution
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wording is copied from lines 135-136 above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the range is non-constant, caching the distribution helps. Either way, caching the result of rand::thread_rng
also helps (slightly).
Some thoughts on this:
Thus I think we should probably reject this PR.
|
I put a wall of text on the issue thread 😅 |
@oconnor663 would you like to update this? #1505 renamed several methods; #1506 renamed I don't think we need
Documentation should in my opinion be relatively short, e.g. copy the first line of the |
@newpavlov any comments before I merge this? I think it's acceptable now. |
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] | ||
#[inline] | ||
#[track_caller] | ||
pub fn random_ratio(numerator: u32, denominator: u32) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't quite like this name, but I guess it's better to handle separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't much like any of the suggested alternatives, but feel free to open a new PR or re-open #1503 for this.
We should remove "shuffle" from the PR name as well |
rand::range
and rand::shuffle
as top-level helper functionsrand::range
, random_bool, random_iter, random_ratio, fill as top-level helper functions
Thanks for landing this! And apologies for disappearing 😅 I agree with @newpavlov that the names |
Original issue: #989
In that issue, @newpavlov originally proposed several new helpers:
gen_range
fill
gen_bool
/gen_ratio
choose
/choose_mut
shuffle
As I started playing with implementing these, I narrowed it down to two functions that seemed the most useful/obvious to me:
range
: The helper I most frequently wish I had. A flexible way to get a random float or an index into a slice. A "pit of success" around therandom::<usize>() % len
mistake. Good integration with Rust's native range syntax. Renamed fromgen_range
, mainly because it's more convenient to type/read, and also to give more space to the upcominggen
keyword.shuffle
: An obvious/straightforward API. Useful in small examples, tests, etc.In my mind, the case for these others is less compelling:
fill
: The most common use case might be generating 128-bit or 256-bit seeds/keys, butrandom()
already works for arrays up to size 32.gen_bool
: Also doable by comparing a float torandom()
, and that alternative might be easier to read.choose
: Useful, but you want two or three different variants (&T
, and&mut T
, and maybeT: Copy
). Easier to get an index fromrange()
and let the caller decide what to do with it.So in this PR I've implemented
rand::range
andrand::shuffle
. Very interested to get other folks' thoughts.