-
-
Notifications
You must be signed in to change notification settings - Fork 433
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
Add a module with helper functions #989
Comments
Is this primarily because We should be careful about adding more convenience functions (complicating the API for ease-of-use), and also realise that we can't be all-things-to-everyone (some people really care about minimising dependencies/size for whatever reason; |
There are a lot of crates like this, implementing some subset of Rand's functionality. There does not seem to be any convergence on a "quick and dirty" randomness crate, with every author creating their own variant. I don't see what we can do about this code duplication. How popular are these crates? I'm not sure that there is a risk of an ecosystem split. Looking at fastrand, it seems the main API simplification is getting rid of |
Don't forget that helper functions not only shorter (due to the implicitness of Since those functions are trivial and will be kept in a separate module, I don't think they will be perceived as an API complication and additional maintenance burden will be minuscule. Judging by a cursory look at these search results, I would say that the proposed helper functions will cover more than half of explciit
I wouldn't call it a convenience wrapper. It's an extension trait which implements additional functionality. |
Not really, it only wraps |
There is also good reason to keep
The main drawback IMO is that in the docs there will be a long-ish section at the bottom of all these functions. But I wouldn't want to remove let mut rng = thread_rng();
let dist = Uniform::from(1..11);
let sample = dist.sample(&mut rng); Conclusion: I see only very minor advantages and drawbacks — so either option sounds acceptable. |
So to resolve this, we would have to implement the following? Via
New methods for
|
My opinion is that we should not attempt to "resolve" this issue without more feedback on what users want and why. Of the above, I don't see anything wrong with adding Adding methods like One thing we can't compete with Ironically the selling point of |
I was hoping more people might pitch in with what they would / would not like to see. So, here's my opinion (again):
Regarding (1): The Regarding (3): Should we impl |
I've always felt that Rust needs a crate for "simple" random number generation. It's definitely hard to draw the line for exactly what to include in such a crate though. To answer the question in #989 (comment), I think the goal of such a crate would primarily be "easy to learn" rather than "few characters to type". To accomplish this I would heavily bias towards having few functions in the API. My proposal would be to include just
And that these functions would use a cryptographically-secure algorithm and optimized for precision to the extent rand is right now (i.e. use exclusion zones for integer generation, but just 53 bits of precision for floats) Most other commonly needed things can be easily and intuitively built on top of those functions. Possibly more functions than the above should have convenience APIs. But it's always easy to add more over time. However I don't think the Honestly I think But I think that the |
Interesting direction: adding |
This issue is stale. Personally I think we should just point new users at the Getting started guide. Close? |
I've been thinking about this for years, but I only just now decided to go digging for it in the issues list :) I occasionally make use of If I wanted to make a biased correctness/security argument for this, I'd point out that it's very convenient but of course wrong to generate a small range integer like this: let x = rand::random::<u32>() % 10; It would be quite nice if instead we could tell people to do this: let x = rand::range(0..10); |
You could import like this? use rand::{random, prelude::*}; In general, using trait functions without importing is a pain (but possible with fully-qualified paths). I suppose we could add functions like |
I just opened #1488 as a proof-of-concept. Would love to get other people's thoughts. |
My thoughts (from #1488) are that we should instead make Potentially also add these inherent methods (selected subsets of
Potentially, we could also remove The above additions to the API are mostly hidden under |
Hmm, thinking some more about what I'm really going for here... Forgive me for a wall of text :) The short version is that I think there are two separate papercuts. The first is that you have to import the trait, but the second is that you have to understand what The long version of that same thought: As you know much better than I do, Here's how it reads when Chapter 2 gets to
If we made the
But imagine if it could be this short:
One question I'd be particularly happy to shield Day 1 beginners from is "What is a thread?" I could imagine some compromise proposals along that line. Maybe we could define Another option we could consider (with "we" here maybe being The Book more than I'm positively biased here by my own experiences with the |
This hides the source of randomness. For some people & usages that's completely fine, but it can also be confusing, especially if someone can't work out why they have to do several other steps (import a trait and explicitly provide the RNG) just to change the RNG.
The documentation for |
This was a good suggestion by @dhardy: rust-random/rand#989 (comment)
Good point, done! BLAKE3-team/BLAKE3@08cb01c |
To update my thoughts on this: Making RNG methods inherent using macros as in #1492 is a little messy and not extensible to anything outside of I approve of the idea of adding I dislike We could implement I think perhaps we should rename |
Ooo I love that idea. |
Name changes have been pushed off to #1503. Pending this, we can add Lets not add I'm still on-the-fence regarding rename
On the whole, I like the idea of this rename, after removing from the prelude. |
No worries :) If we end up sending most beginners to |
This is done (#1488). |
One of the major
rand
critiques, which I've recently seen, argues that it makes hard to write "quick and dirty" code. This motivates creation of crates likefastrand
, which could lead to an unfortunate ecosystem split and overall code duplication.We already have the
random
helper function and I think it could be worth to add other helper functions as well. To make docs less cluttered we probably should keep them in a separate top-level module (something likehelpers
?). With the potential migration to ChaCha8/12, those functions can be simple wrappers aroundthread_rng
and be fast enough for all, but most demanding use-cases. One of side-effects of using helpers is that trait imports are no longer needed for simple use-cases, so it could be seen as a work-around until something like rust-lang/rfcs#2375 gets added to the language.A preliminary list of helper functions:
random
(Rng::gen
)gen_range
(ideally should be consistent withgen_range
, see gen_range doesn't take a Range #744)fill
gen_bool
/gen_ratio
choose
/choose_mut
shuffle
We could tweak naming a bit to make it a bit more natural.
cc @stjepang
The text was updated successfully, but these errors were encountered: