-
Notifications
You must be signed in to change notification settings - Fork 424
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
Proposal: Random module interface changes #14589
Comments
RandomStream.doSomething(..., algorithm=RNG.PCG) |
Note that currently |
Scanning through the implementations of these convenience functions, I don't think there's a need to change them. These are fine because each function only creates a function-local random stream; these functions truly are a convenience for what the user could have done by hand in only one or two extra lines of code.
I don't really care enough about what this module should look like, so feel free to ignore my suggestions. |
At present I don't think we should change anything about RandomStream. I really don't like the direction this is going. I think the whole discussion is confused about the purpose of a factory function. If we wanted As an alternative to an interface, we could consider making RandomStream be a parent class. We could do that now. Long ago, when I introduced PCGRandomStream, I didn't want to do that, for one reason or another. At the very least, if people actually used Note also that if we were to make In particular the Random module has an idea of a default random stream. That default RNG should be easy to use. If you can't get it with |
After some offline discussions, we currently plan to hold off on this until we converged on a wider design discussions for standard library functions. I will close until this becomes a bigger priority unless there is any objections. |
I like the "
I don't have a strong opinion, but would be fine leaving it open as well. |
I'll remark that |
Eh. I take that back. You can make it work without it looking terrible. Just don't define the factory method inside the interface because it's not strictly part of the interface. |
I am not sure how much I myself will be supportive of this proposal after some potential larger-scale style adjustments in the standard modules. I am closing this for now. |
This issue is to propose small interface changes in the Random module.
Background
Under #14291, which was originally about creating a convention for standard
factory functions, we started discussing the
Random
module design and how itsinterface can be adjusted to an agreed upon convention for factory functions.
Here are some relevant details about the things we have in the Random module as
today:
PCGRandomStream
NPBRandomStream
RandomStream
that aliases the default RNG (PCG, see below)enum RNG { PCG = 1, NPB = 2 }
param defaultRNG = RNG.PCG
functions. These allow the user to create a random stream, or use some
Random functionality without jumping through some hoops:
createRandomStream(...., algorithm=defaultRNG)
fillRandom(...., algorithm=defaultRNG)
permutation(...., algorithm=defaultRNG)
shuffle(...., algorithm=defaultRNG)
In #14291, the most support having the convention of
type.create()
for factory functions, which
createRandomStream
does not adhere to. (There isalso an open PR to change all the remaining factory functions in the standard
library to use that convention: #14449).
createRandomStream
's merit is that itparametrizes the algorithm. One way of making it comply with the
convention is to make it
RandomStream.create
, but it would not be very usefulin the existing interface because
RandomStream
is just an alias and this wouldhave an identical behavior to
new [PCG]RandomStream()
.Proposal
Based on my interpretation on the prior discussion under #14291, I propose we do
(all) the following:
Remove
createRandomStream
from the random module.Make
defaultRNG
aconfig param
(I thought I did that before, but I didn't).Add module-level
getRandomStreamType(param algorithm)
to get the typeassociated with a given RNG algorithm.
This is arguably an uglier way of parametrizing the algorithm. But I am not
sure how common that is in the real world applications. I cannot think of any
use case myself. So I'd be fine with that.
Make other module-level helpers type methods on RNG types.
3a. Change
fillRandom
name tofill
, so that we have[PCG|NPB]RandomStream.fill
I am not sure how this would play out when/if module-level random streams are
added (Simplify the construction of simple random numbers #7225), these may be confusing to the user:
The above items are more-or-less mutually exclusive, so maybe a subset of them
is also a good approach.
Code Samples
Creating a random stream
Utility functions
The text was updated successfully, but these errors were encountered: