gpt4 book ai didi

python - Python 3 的 random.SystemRandom.randint 有错误,还是我使用不正确?

转载 作者:太空狗 更新时间:2023-10-29 20:42:04 26 4
gpt4 key购买 nike

>>> import random
>>> random.SystemRandom.randint(0, 10)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
random.SystemRandom.randint(0, 10)
TypeError: randint() missing 1 required positional argument: 'b'

尽管 os.urandom,SystemRandom 应该给出随机数,randint 的工作方式与普通 randrange 相同:

>>> print(random.SystemRandom.randint.__doc__)
Return random integer in range [a, b], including both end points.

在 IDLE 中,当我输入它时会出现一个小的弹出建议

`random.SystemRandom.randint(self, a, b)`

我想是这个原因。我不是很擅长使用类和理解它们是如何工作的,但第一个参数似乎是作为 self 传递的,而它应该是 a。我从来没有真正理解为什么 self 在它甚至不是关键字的情况下被使用,以及它应该如何正常工作,但它通常确实如此。

我是不是做错了,或者我应该在应该做这样的事情时向 Python 基金会报告这个?

最佳答案

random.SystemRandom 是一个类。需要实例化;

In [5]: foo = random.SystemRandom()

In [6]: foo.randint(0, 10)
Out[6]: 0

完整的docstring给出了提示;

In [12]: random.SystemRandom.randint?
Signature: random.SystemRandom.randint(self, a, b)
Docstring: Return random integer in range [a, b], including both end points.
File: /usr/local/lib/python3.4/random.py
Type: function

self参数表示这个randintSystemRandom的一个方法

关于python - Python 3 的 random.SystemRandom.randint 有错误,还是我使用不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com