我试图通过检查一些旧的来源到底部。我怀疑randint是在Python的长整数之前实现的:意思是如果你想要一个包含INT_MAX的随机数,你需要调用random.randrange(0,INT_MAX 1),这会溢出并产生参数(0, 0)或(0,INT_MIN)。
然而,看起来甚至the Python 1.5.2 sources,在Lib / whrandom.py我们看到:
#
# Get a random integer in the range [a, b] including both end points.
# (Deprecated; use randrange below.)
#
def randint(self, a, b):
return self.randrange(a, b+1)
whrandom.randint继续在2.0,2.1,2.2和2.3中被弃用;但random.randint在2.1被标记为弃用,但在2.2不再标记为弃用。
另外,random.py from version 2.1是random.randint的docstring中的第一个注释:
def randrange(self, start, stop=None, step=1, int=int, default=None):
"""Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int' and 'default' arguments.
"""
唯一可用的源比它早the 0.9.1 source,据我所知,randint没有实现在那一点。
因此,我得出结论,包括端点的randint的推理在这一点上仅仅是Guido本身已知;给出从Python 2.1的docstring,它听起来像原因可能是一个简单的错误。