简体   繁体   English

生成随机整数时无法包含上限

[英]Unable to include upper bound when generating random integer numbers

I am writing a method that will generate an unsigned int between 1 and 6 (boundaries included). 我正在编写一个方法,它将生成1到6之间的unsigned int(包括边界)。 The current method I have is below. 我现有的方法如下。

        private static Random random = new Random();
        ...
        private static uint GetRandomChannel()
        {
            return Convert.ToUInt32(random.Next(1, 6));
        }

I've run this method a thousand times and I get numbers 1 through 5 but never get 6. Why is this happening and how can I fix it? 我已经运行了这个方法一千次,我得到数字1到5但从未得到6.为什么会发生这种情况,我该如何解决?

The second parameter to random.Next() is an exclusive upper bound. random.Next()的第二个参数是一个独占上限。

Parameters 参数

minValue : The inclusive lower bound of the random number returned. minValue :返回的随机数的包含下限。

maxValue: The exclusive upper bound of the random number returned. maxValue:返回的随机数的独占上限。 maxValue must be greater than or equal to minValue . maxValue必须大于或等于minValue

Return value 返回值

A 32-bit signed integer greater than or equal to minValue and less than maxValue ; 大于或等于minValue且小于maxValue的32位有符号整数; that is, the range of return values includes minValue but not maxValue . 也就是说,返回值的范围包括minValue但不包括maxValue If minValue equals maxValue , minValue is returned. 如果minValue等于maxValue ,则返回minValue

This means that random.Next(1, 6) will only return values n in the range 1 <= n < 6 . 这意味着random.Next(1, 6)将仅返回1 <= n < 6范围内的值n

So for your die rolling simulation you will need to use 因此,对于您的模具滚动模拟,您将需要使用

random.Next(1, 7)

Note : The design of this API is odd. 注意 :此API的设计很奇怪。 It has special case handling for minValue == maxValue which seems to needlessly complicate the API. 它具有minValue == maxValue特殊情况处理,似乎不必要地使API复杂化。 If I had designed this API I would have made both parameters be inclusive limits. 如果我设计了这个API,我会将这两个参数都包含在内。 This would have resulted in a pleasing symmetry and would have allowed random numbers that cover the full range of int . 这将导致令人愉悦的对称性,并允许覆盖整个int范围的随机数。

According to MSDN, the upper bound is exclusive , while the lower bound is inclusive . 根据MSDN,上限是独占的 ,而下限是包含的

Random.Next Method (Int32, Int32) Random.Next方法(Int32,Int32)

So your code shoudl be: 所以你的代码应该是:

return Convert.ToUInt32(random.Next(1, 7));

根据此处的MSDN文档, random.Next函数返回的值严格小于MaxValue (在您的情况下为6)。

You can't "fix" this; 你无法“修复”这个; it's just what this method means to do: 这就是这个方法的意思:

A 32-bit signed integer greater than or equal to zero and less than MaxValue. 大于或等于零且小于MaxValue的32位有符号整数。

So if you want to generate a random integer in [a, b], you need to use .Next(a, 1 + b) . 因此,如果要在[a,b]中生成随机整数,则需要使用.Next(a, 1 + b)

According to the method documentation , the lower bound is inclusive and the upper bound is exclusive. 根据方法文档 ,下限是包含的,上限是独占的。 That means that random.Next(lower, upper) will return the lower number, but it is guaranteed to never return the upper one. 这意味着random.Next(lower,upper)将返回较低的数字,但保证永远不会返回较高的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM