Home > Backend Development > C++ > What's the Optimal LINQ Approach for Getting a Random Subset?

What's the Optimal LINQ Approach for Getting a Random Subset?

Barbara Streisand
Release: 2024-12-31 19:58:16
Original
482 people have browsed it

What's the Optimal LINQ Approach for Getting a Random Subset?

Optimal LINQ Extension for Random Sub-Collection Extraction

In the world of programming, it's often necessary to extract a random subset from a given collection. For LINQ enthusiasts, you might be wondering about the optimal way to achieve this.

One approach is to utilize the .Shuffle() extension method, which performs the Fisher-Yates-Durstenfeld shuffle. This method effectively randomizes the order of elements in a collection, making it an ideal solution for creating a random sub-collection.

To illustrate its usage, let's assume you have a collection named yourCollection with N items and want to extract n random items where n <= N. Here's how you can implement this using the .Shuffle() method:

var randomItems = yourCollection.Shuffle().Take(n);
Copy after login

This code first shuffles the elements in yourCollection using the .Shuffle() method. It then uses the .Take() method to extract the first n items from the shuffled collection. The result is a random sub-collection of count n.

Here's an example of implementing the .Shuffle() extension method yourself:

public static class EnumerableExtensions
{
    public static IEnumerable Shuffle(this IEnumerable source)
    {
        return source.Shuffle(new Random());
    }

    public static IEnumerable Shuffle(
        this IEnumerable source, Random rng)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));
        if (rng == null)
            throw new ArgumentNullException(nameof(rng));

        return source.ShuffleIterator(rng);
    }

    private static IEnumerable ShuffleIterator(
        this IEnumerable source, Random rng)
    {
        var buffer = source.ToList();
        for (int i = 0; i < buffer.Count; i++)
        {
            int j = rng.Next(i, buffer.Count);
            yield return buffer[j];

            buffer[j] = buffer[i];
        }
    }
}

By utilizing this extension method, you can easily shuffle and extract a random sub-collection of any size from within a LINQ query. This technique offers a convenient and efficient solution for a common data manipulation task.

The above is the detailed content of What's the Optimal LINQ Approach for Getting a Random Subset?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template