Created
August 15, 2013 00:23
-
-
Save monyskynet/6237152 to your computer and use it in GitHub Desktop.
Random items in the list
From http://stackoverflow.com/questions/2019417/access-random-item-in-list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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