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
2009-10-30 18:49:43
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
2009-10-30 20:38:36
+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
2009-10-31 02:27:19
+1
A:
Sorry for ugly code :-), but
var result =yourCollection.OrderBy(p => (p.GetHashCode().ToString() + Guid.NewGuid().ToString()).GetHashCode()).Take(n);
sh1ng
2009-10-31 13:39:41
+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
2009-10-31 18:01:15
+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
2010-07-02 21:19:52