【问题标题】:alternative to else if for random number C#.net [duplicate]如果随机数 C#.net [重复],则替代 else if
【发布时间】:2018-03-07 01:00:48
【问题描述】:

有没有更短的方法可以根据这张动物表检查我从 1 到 100 (catNum) 的随机数?这个看起来还不错,但我还有几个更大的表要处理,我想使用的行数比使用下面的语句要少:

if (catNum < 36) { category = "Urban"; }
        else if (catNum < 51) { category = "Rural"; }
        else if (catNum < 76) { category = "Wild"; }
        else if (catNum < 86) { category = "Wild Birds"; }
        else { category = "Zoo"; }

更多表格示例:

【问题讨论】:

  • 优雅在旁观者眼中,因此基于意见,这是 Stack Overflow 不做的事情。如果您可以提供一个可以判断答案的客观指标(长度、操作次数、速度等),那么这个问题也许可以通过。见How to Askhelp center
  • 好吧,我是这个网站的菜鸟——我希望用更少的行来浏览它们。我应该编辑我的问题吗?
  • 如果这是您的标准,请确保 edit 离开。
  • 希望现在更有意义
  • @gamblePants 您的代码似乎合理。我唯一建议的就是不要把事情排成一行。我不知道您是否在这里为简洁起见,但您的生产代码不应该全部在一行

标签: c# if-statement random


【解决方案1】:

我更喜欢使用这样的东西而不是许多 if/else 一个类别类

class Category
{
    public int Min { get; set; }
    public int Max { get; set; }
    public string Name { get; set; }
}  

初始化类别一次并用您的值填充它

var categories = new List<Category>();

最后是一种解决类别的方法

public static string Get(int currentValue)
{
    var last = categories.Last(m => m.Min < currentValue);
    //if the list is ordered 
    //or
    // var last = categories.FirstOrDefault(m => m.Min <= currentValue && m.Max >= currentValue);
    return last?.Name;
}

【讨论】:

  • 我不确定你的意思,但你可以将列表作为第二个参数发送给方法并对其进行过滤
  • 哎呀抱歉,我删除了第一条评论哈哈,我打算放一个指向我的代码的链接。我想试试这种方式,我会试一试,谢谢
【解决方案2】:

另一种方法是建立一个完整的项目列表,然后您可以按索引随机选择一个:

var categories =
    Enumerable.Repeat("Urban", 35)
        .Concat(Enumerable.Repeat("Rural", 15))
        .Concat(Enumerable.Repeat("Wild", 25))
        .Concat(Enumerable.Repeat("Wild Birds", 10))
        .Concat(Enumerable.Repeat("Zoo", 15))
        .ToArray();

var category = categories[45]; //Rural;

【讨论】:

    【解决方案3】:

    是的,这是一个经过充分研究的问题,并且存在比您已经发现的 if-else 链更有效的解决方案。详情请见https://en.wikipedia.org/wiki/Alias_method

    我的建议是:构造一个表示概率单子的通用接口类型——比如IDistribution&lt;T&gt;。然后编写一个使用别名方法的离散分布实现。 将机制工作封装到分发类中,然后在使用站点,您只需要一个让您进行分发的构造函数,以及一个为您提供分发元素的T Sample() 方法。

    我注意到在您的示例中,您可能有一个贝叶斯概率,即P(Dog | Urban)。概率单子是表示这些事物的理想机制,因为我们将P(A|B) 重新表述为Func&lt;B, IDistribution&lt;A&gt;&gt; 那么我们得到了什么?我们有一个IDistribution&lt;Location&gt;,我们有一个从LocationIDistribution&lt;Animal&gt; 的函数,然后我们认识到我们通过概率单子上的绑定操作将它们放在一起。 这意味着在 C# 中我们可以使用 LINQSelectMany 是对序列的绑定操作,但它也可以用作对任何 monad 的绑定操作!

    现在,鉴于此,做一个练习:什么是 LINQ 中的条件概率运算


    记住目标是使调用站点的代码看起来像正在执行的操作。如果您从离散分布进行逻辑抽样,那么有一个代表离散分布的对象并从中抽样。

    【讨论】:

      【解决方案4】:
      string[] animals = new string[] { "Urban", "Rural", "Wild", "Wild Birds", "Zoo" };
      int[] table = new int[] { 35, 50, 75, 85 };     
      for (int catNum = 10; catNum <= 100; catNum += 10)
      {
          int index = Array.BinarySearch(table, catNum);
          if (index < 0) index = ~index;
          Console.WriteLine(catNum + ": " + animals[index]);
      }
      

      在线运行:https://dotnetfiddle.net/yMeSPB

      【讨论】:

        猜你喜欢
        • 2018-03-29
        • 2013-09-16
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        相关资源
        最近更新 更多