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

[API Proposal]: Random.NextArray function #110903

Closed
CypherPotato opened this issue Dec 23, 2024 · 5 comments
Closed

[API Proposal]: Random.NextArray function #110903

CypherPotato opened this issue Dec 23, 2024 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime

Comments

@CypherPotato
Copy link

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.

public static T NextArray<T> ( this Random random, IReadOnlyList<T> items ) {
    return items [ random.Next ( 0, items.Count ) ];
}

Or within the Random class:

public static T NextArray<T> ( IReadOnlyList<T> items ) {
    return items [ this.Next ( 0, items.Count ) ];
}

In .NET 9 we could extend IReadOnlyList<T> to allow params in the parameter.

API Usage

string [] fruits = [ "Apple", "Mango", "Tomato", "Blueberry" ];
string randomFruit = Random.Shared.NextArray ( fruits );

Console.WriteLine ( randomFruit );

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, as IReadOnlyList<T> does not implement ICollection.SyncRoot.

@CypherPotato CypherPotato added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 23, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 23, 2024
@huoyaoyuan huoyaoyuan added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 23, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

The proposed name is confusing. NextArray means returning an array. The proposed functionality is to return one random element from an array-like.

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 Random.Shuffle. On the other hand, items[random.Next(0, item.Count)] is too trivial.

@CypherPotato
Copy link
Author

CypherPotato commented Dec 23, 2024

@huoyaoyuan NextItem maybe? I didn't thinked so much about the method name, but that's not the issue main objective. And, I disagree, it's not so trivial, I've come across some situations like choosing a welcome message from a list of pre-defined messages. Depending on how it is implemented, it leaves room for small errors which can break the code in runtime.

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, NextArray may not be the best way to do it.

Also, we could allow an overload with IEnumerable<T> to allow a better filtering of the expected result.

@colejohnson66
Copy link

IEnumerable would not work because the size of the enumeration is unbounded. Randomly sampling it is impossible.

@KeterSCP
Copy link

This was previously proposed and rejected in #80651 (comment)

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

4 participants