python random randint_Python:为什么`random.randint(a,b)`返回一个包含`b`的范围?

我试图通过检查一些旧的来源到底部。我怀疑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,它听起来像原因可能是一个简单的错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值