Python 3 的 random.SystemRandom.randint 有错误还是我用错了?

Is there an error in Python 3's random.SystemRandom.randint, or am I using in incorrectly?

>>> 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'

SystemRandom 应该给出随机数,尽管 os.urandomrandint 像正常工作一样 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 基金会报告这种情况?

我想你想要:

import random
r = random.SystemRandom()
print(r.randint(0, 10))

相反。

这是因为您需要创建 random.SystemRandom class 的实例。

但是您可以轻松地使用以下内容:

import random
random.randint(0, 10)

如果您不需要 OS 相关的加密 RNG。

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

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方法

当错误是“TypeError: randint() missing 1 required positional argument: 'b'”时,这是因为编写代码的人可能混淆了他们的

random.randint()

和他们

random.choice()

希望我能帮到你 :D