views:

443

answers:

5

Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N

+5  A: 

Shuffle the collection into a random order and take the first n items from the result.

mquander
Note that you don't have to completely shuffle the collection when n < N. You simply have to iterate through Durstenfeld's algorithm n times, and take the last n items of the (partially) shuffled result.
Dan Blanchard
+18  A: 

Further to mquander's answer and Dan Blanchard's comment, here's a LINQ-friendly extension method that performs a Fisher-Yates-Durstenfeld shuffle:

// take n random items from yourCollection
var randomItems = yourCollection.Shuffle().Take(n);

// ...

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    Random rng = new Random();
    T[] items = source.ToArray();

    for (int n = 0; n < items.Length; n++)
    {
        int k = rng.Next(n, items.Length);
        yield return items[k];

        items[k] = items[n];
    }
}
LukeH
+1 Linqy and efficient. Hats off :)
Dan Blanchard
+1  A: 

Sorry for ugly code :-), but


var result =yourCollection.OrderBy(p => (p.GetHashCode().ToString() + Guid.NewGuid().ToString()).GetHashCode()).Take(n);

sh1ng
+1  A: 

This has some issues with "random bias" and I am sure it's not optimal, this is another possibility:

var r = new Random();
l.OrderBy(x => r.NextDouble()).Take(n);
pst
+2  A: 

Another option is to use OrderBy and to sort on a GUID value, which you can do so using:

var result = sequence.OrderBy(elem => Guid.NewGuid());

I did some empirical tests to convince myself that the above actually generates a random distribution (which it appears to do). You can see my results at Techniques for Randomly Reordering an Array.

Scott Mitchell