Skip to content

Instantly share code, notes, and snippets.

@longtth
Created June 10, 2018 10:15
Show Gist options
  • Save longtth/0d6c2ac285891278d42b75126a343065 to your computer and use it in GitHub Desktop.
Save longtth/0d6c2ac285891278d42b75126a343065 to your computer and use it in GitHub Desktop.
Get an random value from enum
static T RandomEnumValue<T> ()
{
var v = Enum.GetValues (typeof (T));
return (T) v.GetValue (new Random ().Next(v.Length));
}
//-------------------------------------------
//Test:
for (int i = 0; i < 10; i++) {
var value = RandomEnumValue<System.DayOfWeek> ();
Console.WriteLine (value.ToString ());
}
//-------------------------------------------
//Output:
/*
Tuesday
Saturday
Wednesday
Monday
Friday
Saturday
Saturday
Saturday
Friday
Wednesday
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment