为什么能minValue(最小值)== maxValue分别在Random.Next?最小值、分别在、minValue、maxValue

2023-09-06 07:37:44 作者:君子动手不动口

http://msdn.microsoft.com/en-us/library/ 2dx6wyd4.aspx

显然 maxValue(最大值)是独占的上限。因此, Random.Next(2,3)将始终返回2. Random.Next(2,2)也将总是返回2.为什么你觉得他们甚至允许的最小等于最大在这种情况下?这是误导!

Apparently maxValue is an exclusive upper bound. So Random.Next(2,3) will always return 2. Random.Next(2,2) will also always return 2. Why do you suppose they even allow the min to be equal to the max in this case? It's misleading!

推荐答案

看起来只是有点设计不良给我。我同意 maxValue(最大值)应严格比 minValue(最小值)更大 - 它应该抛出一个 ArgumentOutOfRangeException 如果他们是平等的。

Looks like just a bit of poor design to me. I agree that maxValue should be strictly greater than minValue - and it should throw an ArgumentOutOfRangeException if they're equal.

有几个方面随机,我不喜欢 - 一件事,那将是非常不错的,它正确地可插拔(在证据充分的方式),所以你可以选择使用加密安全源的子类。它的可能的现在,但你基本上需要知道太多的执行情况,以及哪些方法调用的其他的:(

There are a few aspects to Random which I dislike - for one thing, it would be really nice to have it properly pluggable (in a well-documented way) so that you could have a subclass using a cryptographically secure source. It's possible now, but you basically need to know too much about the implementation, and which methods call which other ones :(

围绕创建一个新的随机实例每次去圆一个循环时间的陷阱,以及明显的修复(静态变量)不是线程安全的其他烦恼。

The gotchas around creating a new Random instance each time you go round a loop, and the obvious "fix" (a static variable) not being thread-safe are other annoyances.

基本上,我不是非常惊讶地看到另一个小疣:(

Basically, I'm not terribly surprised to see another little wart :(