random.Next(0,5)
Run Code Online (Sandbox Code Playgroud)
它永远不会返回5(但有时会返回0.)为什么?我认为这些只是可以返回的边界值.谢谢
Mar*_*ade 122
在maxValue用于上界的Next()方法是独特的 -the范围包括minValue,maxValue-1,以及两者之间的所有数字.
直接来自文档:
Summary:
Returns a random number within a specified range.
Parameters:
minValue:
The inclusive lower bound of the random number returned.
maxValue:
The exclusive upper bound of the random number returned. maxValue must be
greater than or equal to minValue.
Returns:
A 32-bit signed integer greater than or equal to minValue and less than maxValue;
that is, the range of return values includes minValue but not maxValue. If
minValue equals maxValue, minValue is returned.
Run Code Online (Sandbox Code Playgroud)
如果你看一下这些参数,你就会发现它minValue是包容性的(这就是你的0出现的原因)并且maxValue是独占的(你的5永远不会发生).