Skip to content

Instantly share code, notes, and snippets.

@monyskynet
Created August 15, 2013 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monyskynet/6237152 to your computer and use it in GitHub Desktop.
Save monyskynet/6237152 to your computer and use it in GitHub Desktop.
public static class EnumerableExtension
{
public static T PickRandom<T>(this IEnumerable<T> source)
{
return source.PickRandom(1).Single();
}
public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
{
return source.Shuffle().Take(count);
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
return source.OrderBy(x => Guid.NewGuid());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment