【问题标题】:Adding random values from array with some probability以一定的概率从数组中添加随机值
【发布时间】:2015-05-27 13:24:56
【问题描述】:

假设我有一个 Windows 窗体的代码,当您按下按钮时会生成问候语。

   string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
   string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };

   Random rand = new Random();
   string Greet = Greetings[rand.Next(0, Greetings.Length)];
   string Smile = Smilies[rand.Next(0, Smilies.Length)];
   TextBox.Text = Greet + " " + Smile;
   Clipboard.SetText(TextBox.Text);

如果我想以 X% 的概率添加表情符号怎么办。这样它们就不会一直出现,而是有机会我在代码中设置?有什么好的方法吗?

我想到了这样的事情--

    public void chance (string source, int probability)
    {
        Random chanceStorage = new Random();
        if (probability >= chanceStorage.Next(0, 100))
            TextBox.Text = source;
    }

然后

    TextBox.Text = Greet;
    chance("_" + Smile, X);

这是最优的吗?

【问题讨论】:

  • const int percentageValue = 25; double percentage = (double)percentageValue / 100; if (rand.NextDouble() <= percentage) { \\ do stuff }
  • 将尽可能多的 String.Empty 值添加到您的 Smilies 数组中,直到达到没有您喜欢的表情的概率。
  • @Corak 有一个很好的答案(如果它是一个答案)。
  • 为什么不将表情符号长度除以概率并使用该商生成随机数?如果生成的数字在表情符号的长度范围内,则使用笑脸,否则不使用笑脸。因此,如果您的概率为 25%,4 / 0.25 = 16,您将生成一个 0 - 15 的随机数,让您有 25% 的机会使用笑脸。
  • @ryanyuyu - 完全未经测试,所以我不能“确定”将其作为答案。

标签: c# arrays probability


【解决方案1】:

50% 的机会微笑:

   string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
   string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };

   Random rand = new Random();
   string Greet = Greetings[rand.Next(0, Greetings.Length)];
   string Smile = rand.NextDouble() > 0.5 ? " "+Smilies[rand.Next(0, Smilies.Length)] : string.Empty;
   TextBox.Text = Greet + Smile;

【讨论】:

  • 非常感谢!我唯一要改变的是:0.5 > rand.NextDouble()。这样概率在代码中看起来会更自然
【解决方案2】:

我只需使用random.NextDouble() 在 0、1(仅包括 0)之间生成一个随机双精度数。然后您可以检查概率(作为 0 到 1 之间的值)是否小于生成的数字。

例如

double chance = 0.35; //35%
bool createSmiley = rand.NextDouble() < chance;

这不一定完全精确(因为浮点数的精度有限),但会让你非常接近。并且比仅使用 ints 作为随机数更精确。

demo of the random generation of smileys

另一方面,我会稍微重构您的代码以更好地隔离逻辑。我个人只是尝试操作单个字符串,并在完成所有字符串处理后设置一次 .Text 属性。

【讨论】:

    【解决方案3】:

    我会将您的表情符号数组的长度除以您的概率,以给出要生成的随机数范围。

    4 个表情符号 / 25% 的概率 = 16。因此,您的随机生成器会生成一个 0 到 15 之间的随机数,从而有 25% 的机会使用表情符号。如果生成的数字超出了数组的范围,请不要使用笑脸。

    using System;
    
    public class Program
    {
        public static void Main()
        {
            Random rand = new Random();
            double percentage = rand.NextDouble();
    
            string[] greetings = new[] { "Hi", "Hello", "Howdy!", "Hey" };
            string[] smilies = new[] { ";)", ":)", "=)", ":-)" };
    
            // Get a greeting
            string greet = greetings[rand.Next(0, greetings.Length)];
    
            // Generate the smiley index using the probablity percentage
            int randomIndex = rand.Next(0, (int)(smilies.Length / percentage));
    
            // Get a smiley if the generated index is within the bound of the smilies array
            string smile = randomIndex < smilies.Length ? smilies[randomIndex] : String.Empty;
    
            Console.WriteLine("{0} {1}", greet, smile);
        }
    }
    

    在此处查看工作示例...https://dotnetfiddle.net/lmN5aP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 2022-01-08
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2011-12-25
      • 2011-04-10
      相关资源
      最近更新 更多