Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returning random element of any list of type T
public static class ExtensionMethods
{
public static Random random = new Random();
public static T Random<T>(this IEnumerable<T> list)
{
return list.ToArray().Random();
}
public static T Random<T>(this T[] array)
{
return array[random.Next(0, array.Length - 1)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment