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

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


当前回答

Array values = Enum.GetValues(typeof(Bar));
Random random = new Random();
Bar randomBar = (Bar)values.GetValue(random.Next(values.Length));

其他回答

很多答案都很老了,而且——如果我说错了请纠正我——似乎只涉及一些粗略的概念,比如类型擦除和动态类型转换。然而,正如用户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

下面是一个使用LINQ的扩展方法的替代版本。

using System;
using System.Linq;

public static class EnumExtensions
{
    public static Enum GetRandomEnumValue(this Type t)
    {
        return Enum.GetValues(t)          // get values from Type provided
            .OfType<Enum>()               // casts to Enum
            .OrderBy(e => Guid.NewGuid()) // mess with order of results
            .FirstOrDefault();            // take first item in result
    }
}

public static class Program
{
    public enum SomeEnum
    {
        One = 1,
        Two = 2,
        Three = 3,
        Four = 4
    }

    public static void Main()
    {
        for(int i=0; i < 10; i++)
        {
            Console.WriteLine(typeof(SomeEnum).GetRandomEnumValue());
        }
    }           
}

两个 一个 四个 四个 四个 三个 两个 四个 一个 三个

你可以这样做:

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

不需要存储数组

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

就我个人而言,我是一个扩展方法的粉丝,所以我会使用这样的东西(虽然不是真正的扩展,但它看起来很相似):

public enum Options {
    Zero,
    One,
    Two,
    Three,
    Four,
    Five
}

public static class RandomEnum {
    private static Random _Random = new Random(Environment.TickCount);

    public static T Of<T>() {
        if (!typeof(T).IsEnum)
            throw new InvalidOperationException("Must use Enum type");

        Array enumValues = Enum.GetValues(typeof(T));
        return (T)enumValues.GetValue(_Random.Next(enumValues.Length));
    }
}

[TestClass]
public class RandomTests {
    [TestMethod]
    public void TestMethod1() {
        Options option;
        for (int i = 0; i < 10; ++i) {
            option = RandomEnum.Of<Options>();
            Console.WriteLine(option);
        }
    }

}