Skip to content

Instantly share code, notes, and snippets.

@dejanstojanovic
Created February 6, 2015 18:12
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 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