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

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


当前回答

下面是一个使用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());
        }
    }           
}

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

其他回答

这是它的一个通用函数。 保持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>();

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

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);
        }
    }

}

现代的回答:把这个回答和它的注释结合起来:

public static class RandomExtensions
{
    private static Random Random = new Random();

    public static T GetRandom<T>() where T : struct, Enum
    {
        T[]? v = Enum.GetValues<T>();
        return (T)v.GetValue(Random.Next(v.Length));
    }
}

改编为随机类扩展:

public static class RandomExtensions
{   
    public static T NextEnum<T>(this Random random)
    {
        var values = Enum.GetValues(typeof(T));
        return (T)values.GetValue(random.Next(values.Length));
    }
}

用法示例:

var random = new Random();
var myEnumRandom = random.NextEnum<MyEnum>();

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