Skip to content

Instantly share code, notes, and snippets.

@dejanstojanovic
Created February 6, 2015 18:12
Show Gist options
  • Save dejanstojanovic/d53c2cd5e6c87d90b222 to your computer and use it in GitHub Desktop.
Save dejanstojanovic/d53c2cd5e6c87d90b222 to your computer and use it in GitHub Desktop.
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