给定c#中的任意枚举,我如何选择一个随机值?

(我没有在SO上找到这个非常基本的问题。我将在一分钟内发布我的答案作为任何人的参考,但请随时发布你自己的答案。)


当前回答

使用枚举。GetValues检索所有值的数组。然后选择一个随机的数组项。

static Random _R = new Random ();
static T RandomEnumValue<T> ()
{
    var v = Enum.GetValues (typeof (T));
    return (T) v.GetValue (_R.Next(v.Length));
}

测试:

for (int i = 0; i < 10; i++) {
    var value = RandomEnumValue<System.DayOfWeek> ();
    Console.WriteLine (value.ToString ());
}

->

Tuesday
Saturday
Wednesday
Monday
Friday
Saturday
Saturday
Saturday
Friday
Wednesday

其他回答

这是它的一个通用函数。 保持RNG创建在高频代码之外。

public static Random RNG = new Random();

public static T RandomEnum<T>()
{  
    Type type = typeof(T);
    Array values = Enum.GetValues(type);
    lock(RNG)
    {
        object value= values.GetValue(RNG.Next(values.Length));
        return (T)Convert.ChangeType(value, type);
    }
}

使用的例子:

System.Windows.Forms.Keys randomKey = RandomEnum<System.Windows.Forms.Keys>();

使用枚举。GetValues检索所有值的数组。然后选择一个随机的数组项。

static Random _R = new Random ();
static T RandomEnumValue<T> ()
{
    var v = Enum.GetValues (typeof (T));
    return (T) v.GetValue (_R.Next(v.Length));
}

测试:

for (int i = 0; i < 10; i++) {
    var value = RandomEnumValue<System.DayOfWeek> ();
    Console.WriteLine (value.ToString ());
}

->

Tuesday
Saturday
Wednesday
Monday
Friday
Saturday
Saturday
Saturday
Friday
Wednesday

很多答案都很老了,而且——如果我说错了请纠正我——似乎只涉及一些粗略的概念,比如类型擦除和动态类型转换。然而,正如用户Yarek T指出的那样,Enum的通用重载不需要这样做。getvalue:

static Random random = new Random();

// Somewhat unintuitively, we need to constrain the type parameter to
// both struct *and* Enum - struct is required b/c the type can't be
// nullable, and Enum is required b/c GetValues expects an Enum type.
// You'd think that Enum itself would satisfy the non-nullable
// constraint, but alas, me compiler tells me otherwise - perhaps
// someone more knowledgeable can explain why this is in a comment?
static TEnum RandomEnumValue<TEnum>() where TEnum : struct, Enum
{
    TEnum[] vals = Enum.GetValues<TEnum>();
    return vals[random.Next(vals.Length)];
}

或者,像borja garcia的答案一样,我们甚至可以把它写成随机类的扩展

public static class RandomExtensions
{   
    public static TEnum NextEnumValue<TEnum>(this Random random)
        where TEnum : struct, Enum
    {
        TEnum[] vals = Enum.GetValues<TEnum>();
        return vals[random.Next(vals.Length)];
    }
}

我们可以从mafu的答案中进行相同的测试:

Random random = new Random();
for (int i = 0; i < 10; i++) {
    var day = random.NextEnumValue<System.DayOfWeek>();
    Console.WriteLine(day.ToString());
}

潜在产出:

Thursday
Saturday
Sunday
Sunday
Sunday
Saturday
Wednesday
Monday
Wednesday
Thursday

你可以这样做:

var rnd = new Random();
return (MyEnum) rnd.Next(Enum.GetNames(typeof(MyEnum)).Length);

不需要存储数组

叫Enum.GetValues;这将返回一个数组,表示枚举的所有可能值。从这个数组中随机选择一个项目。将该项强制转换回原始enum类型。