-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Random.NextArray function #110903
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime |
The proposed name is confusing. In real-world usage, there are also usage that requires no duplications while randomly picking items. This is more complicated and can be helped by |
@huoyaoyuan In the case of duplicates, it would only make sense if you were getting several random items from an array in series, not something momentary. If it's the first case and you need to filter results, Also, we could allow an overload with |
|
This was previously proposed and rejected in #80651 (comment) |
Background and motivation
The motivation for this issue is that I needed to perform a similar task, but I couldn't find any native way to do it. I wondered why there isn't something ready in the System.Random class, which already has helpers for generating random data, and I believe this suggestion makes sense.
API Proposal
The behavior is simple and straightforward. The following extension function does exactly what I am proposing.
Or within the Random class:
In .NET 9 we could extend
IReadOnlyList<T>
to allowparams
in the parameter.API Usage
Alternative Designs
No response
Risks
Perhaps a risk would be trying to access an item after it has been removed and the
Count
has been reduced shortly after accessed. A lock would solve this, but we would have to change the input type, asIReadOnlyList<T>
does not implementICollection.SyncRoot
.The text was updated successfully, but these errors were encountered: