Random sort on IEnumerable object

Quick extension on an IEnumerable object to return the collection in a random order:

public static IEnumerable<T> Random<T>(this IEnumerable<T> source)
{
	return source.OrderBy(x => Guid.NewGuid());
}

For example, to return a random set of 5 items from the collection:

var randomSet = myEnumerable.Random().Take(5);

If anyone knows of a better way, let me know :)

Leave a Comment

* are Required fields

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 5 bad guys.