【问题标题】:Getting specific random number获取特定的随机数
【发布时间】:2017-10-27 09:34:02
【问题描述】:

我想生成 (1 到 6) 之间的随机数,有没有办法改变获得数字 6 的机会比其他数字多?

例如这个代码

    private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
    {
        Random u = new Random();
        m = u.Next(1,6);
        label2.Text = m.ToString();
    }

【问题讨论】:

标签: c# random numbers


【解决方案1】:

取决于可能性有多大。一种简单的方法(但不是很灵活)如下:

private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
{
    Random u = new Random();
    m = u.Next(1,7);
    if (m > 6) m = 6;
    label2.Text = m.ToString();
}

【讨论】:

    【解决方案2】:

    那不会是随机的。如果你想加重它的重量,这样你就可以得到 6 的一半,你可以这样做:

    m = u.Next(1,2);
    if(m == 2)
    {
    label2.Text = "6";
    }
    else
    {
    label2.Text = u.Next(1,5).ToString();
    }
    

    根据您想要的权重,您可以更改它-> 3 而不是 2 获得 33.33% 的权重,依此类推。否则,正如评论者所说,您必须研究概率分布以获得更优雅的数学解决方案。

    【讨论】:

      【解决方案3】:

      p 是任何1..5 数字的概率,1 - p6 的概率:

      //DONE: do not recreate Random
      private static Random s_Generator = new Random();
      
      private void pictureBox5_MouseClick(object sender, MouseEventArgs e) {
        const double p = 0.1; // each 1..5 has 0.1 probability, 6 - 0.5 
      
        // we have ranges [0..p); [p..2p); [2p..3p); [3p..4p); [4p..5p); [5p..1)
        // all numbers 1..5 are equal, but the last one (6)
        int value = (int) (s_Generator.NexDouble() / p) + 1;
      
        if (value > 6) 
           value = 6;        
      
        label2.Text = value.ToString();
      }
      

      【讨论】:

        【解决方案4】:

        如果您想要 1...5 的完全随机分布并且只是偏斜的 6,那么 Dmitry 似乎是最好的。

        如果你想歪曲所有数字,那么试试这个:

        • 创建一个包含 100 个元素的数组。
        • 根据您希望数字出现的频率按比例填充数字 1-6。 (将其中的 33 个设为 6,如果您希望 6 个出现 1/3 的时间。四个 4 意味着它只会出现 25 个卷中的一个,等等。每个数字的 15 或 16 个将使其分布均匀,所以从那里调整计数)
        • 然后从 0...99 中选择一个数字并使用数组元素中的值。

        【讨论】:

          【解决方案5】:

          您可以以获取数组中每个数字的百分比来定义可能性:

          /*static if applicable*/
          int[] p = { (int)Math.Ceiling((double)100/6), (int)Math.Floor((double)100/6), (int)Math.Ceiling((double)100/6), (int)Math.Floor((double)100/6), (int)Math.Ceiling((double)100/6), (int)Math.Ceiling((double)100/6) };
          ////The array of probabilities for 1 through p.Length
          Random rnd = new Random();
          ////The random number generator
          int nextPercentIndex = rnd.Next() % 100; //I can't remember if it's length or count for arrays off the top of my head
          ////Convert the random number into a number between 0 and 100
          int TempPercent = 0;
          ////A temporary container for comparisons between the percents
          int returnVal = 0;
          ////The final value
          for(int i = 0; i!= p.Length; i++){
              TempPercent += p[i];
              ////Add the new percent to the temporary percent counter
              if(nextPercentIndex <= TempPercent){
                  returnVal = i + 1;
                  ////And... Here is your value
                  break;
              }
          }
          

          希望这会有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-08-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-02-14
            • 1970-01-01
            • 2012-07-29
            • 1970-01-01
            相关资源
            最近更新 更多