从选定组中获取随机枚举

发布于 2024-09-05 06:46:22 字数 117 浏览 4 评论 0原文

给定一组大约 20 个我无法修改的枚举。

我正在寻找一个优雅的解决方案来从特定样本(即 2、7、18)生成随机枚举,

我可以将它们放入数组列表中,但我想我会问是否还有其他我可以尝试的东西。

Given a group of about 20 enums that I cannot modify.

Im looking for an elegant solution to generate a random enum from a specific sample (ie, 2, 7, 18)

I could put these into an arraylist, but thought I would ask if there is something else I could try.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鹊巢 2024-09-12 06:46:23
    enum days
    {
        sunday,monday,tuesday,wednesday,thursday,friday,saturday
    }
    static void Main(string[] args)
    {
        Random r = new Random();
        Console.WriteLine(Enum.GetNames(typeof(days))[r.Next(1, 7)]);    
        Console.ReadKey();
    }
    enum days
    {
        sunday,monday,tuesday,wednesday,thursday,friday,saturday
    }
    static void Main(string[] args)
    {
        Random r = new Random();
        Console.WriteLine(Enum.GetNames(typeof(days))[r.Next(1, 7)]);    
        Console.ReadKey();
    }
枉心 2024-09-12 06:46:23

最简单的解决方案是将您想要选择的枚举放入某种数组或列表中,然后从中随机选择。

如果您只是从完整定义中随机选择,则必须检查答案并拒绝那些您不想要的答案。

The simplest solution is to put those enums you want to choose from into an array or list of some kind and randomly pick from that.

If you just randomly pick from the the full definition you'll have to check the answer and reject those you don't want.

海风掠过北极光 2024-09-12 06:46:22

如果您的值都是同一类型:

MyEnum[] values = { MyEnum.Value2, MyEnum.Value7, MyEnum.Value18 };
Random random = new Random();
MyEnum randomValue = values[random.Next(values.Length)];

If your values are all of the same type:

MyEnum[] values = { MyEnum.Value2, MyEnum.Value7, MyEnum.Value18 };
Random random = new Random();
MyEnum randomValue = values[random.Next(values.Length)];
摘星┃星的人 2024-09-12 06:46:22

您可以使用以下命令获取给定枚举类型的所有值:

var values = Enum.GetValues(typeof(MyEnum));

You can get all the values for a given enum type with:

var values = Enum.GetValues(typeof(MyEnum));
吃兔兔 2024-09-12 06:46:22

我开发了一个快速控制台应用程序,可以执行类似的操作,但无法证明它的性能如何:)

using System;

namespace RandomEnum
{
    class Program
    {
        private enum TestEnum
        {
            One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
        };

        static void Main(string[] args)
        {
            string[] names = Enum.GetNames(typeof (TestEnum));

            Random random = new Random();

            int randomEnum = random.Next(names.Length);

            var ret = Enum.Parse(typeof (TestEnum), names[randomEnum]);

            Console.WriteLine(ret.ToString());
        }
    }
}

I knocked up a quick console app that does something like this, can't attest to how performant it is though :)

using System;

namespace RandomEnum
{
    class Program
    {
        private enum TestEnum
        {
            One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
        };

        static void Main(string[] args)
        {
            string[] names = Enum.GetNames(typeof (TestEnum));

            Random random = new Random();

            int randomEnum = random.Next(names.Length);

            var ret = Enum.Parse(typeof (TestEnum), names[randomEnum]);

            Console.WriteLine(ret.ToString());
        }
    }
}
余生再见 2024-09-12 06:46:22

为什么搞得这么复杂...

private static T GetRandomEnum<T>()
{
    return Enum.GetValues(typeof(T)).Cast<T>().OrderBy(e => Guid.NewGuid()).First();
}

用法:

GetRandomEnum<MyEnumType>()

Why do it so complicated...

private static T GetRandomEnum<T>()
{
    return Enum.GetValues(typeof(T)).Cast<T>().OrderBy(e => Guid.NewGuid()).First();
}

Usage:

GetRandomEnum<MyEnumType>()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文