【问题标题】:C# How to generate a random number in a range, biased toward the low end of the range? [duplicate]C#如何生成一个范围内的随机数,偏向范围的低端? [复制]
【发布时间】:2021-03-02 14:49:04
【问题描述】:

喂! 我的代码是:

    class Program
        {
            static void Main(string[] args)
            {
                for (int x = 0; x < 15000; x++)
                {
                    int minValue = 1;
                    int maxValue = 1400;
                var rand = new Random();

                List<float> weights = new List<float>();
                List<float> numbers = new List<float>();
                for (int i = minValue; i <= maxValue; i++)
                {
                    weights.Add((int)Math.Pow(i, 0.4));
                    numbers.Add(i);
                }
                weights.Reverse();

                int weightSum = 0;
                foreach (int weight in weights)
                {
                    weightSum += weight;
                }

                List<int> possibleNumberList = new List<int>();

                float randomNumber = rand.Next(minValue, weightSum);

                float leastDifference = 2000000000f;
                int leastDifferenceNumberIndex = -1;
                for (int weightIndex = 0; weightIndex < weights.Count - 1; weightIndex++)
                {
                    if (Math.Abs(weights[weightIndex] - randomNumber) == leastDifference)
                    {
                        leastDifference = Math.Abs(weights[weightIndex] - randomNumber);
                        leastDifferenceNumberIndex = weightIndex;

                        possibleNumberList.Add(weightIndex);
                    }
                    else if (Math.Abs(weights[weightIndex] - randomNumber) < leastDifference)
                    {
                        leastDifference = Math.Abs(weights[weightIndex] - randomNumber);
                        leastDifferenceNumberIndex = weightIndex;
                        possibleNumberList = new List<int>();
                        possibleNumberList.Add(weightIndex);
                    }
                }

                var randFromListR = new Random();

                int listCunt = possibleNumberList.Count;
                int index = randFromListR.Next(0, listCunt);
                WWriteStringToNewLine(possibleNumberList[index].ToString());
            }
        }
    }

我的目标是在图像中显示一个列表或数组

虽然我的最大值是 1400。

使用我的代码,我只能实现以下输出:

如果我们放大,我们可以看到有一些更高的数字,但只生成了一次。

如果我们将代码的最大值设置为 10,则输出如下:

{3: 2837, 0: 2813, 4: 2781, 2: 2761, 1: 2759, 5: 273, 6: 264, 7: 262, 8: 250}

我可以对此代码进行哪些更改以使其正常工作?你有什么建议?你甚至可以给我一个完全不同的代码。

【问题讨论】:

  • “偏向低端”这是非常模糊的,可以通过各种不同的方式来实现(基本示例是生成 2 个随机数并只返回最低的一个)。然后你显示一个直方图,但不要说这是否只是一个偏向低端的分布的例子,或者你是否想要那个精确的分布? (找到一个适合的公式是否足够好,或者您想通过对提供的直方图进行采样来绘制随机数?)

标签: c# arrays random integer range


【解决方案1】:

如果您将一个函数应用于您的随机结果,这将改变您的号码重新分区。 我试过这些: Exp(Random.Next(0,10000) / 100d) 将生成一个带有您显然寻求的重新分区的数字

Sqrt(Random.Next(0,10000)) 将生成一个从 0 到 100 的数字,并进行平方根重新分区。

如果需要,可以取整数部分

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2020-07-18
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多