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

标签 python random

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

相关文章:

python - 读取 "pickle"文件时出错,没有名为 'Data' 的模块

python - 根据 Pandas DataFrame 中的权重计算分数

sql - 从表中选择前 N 个随机行,然后按列排序

c++ - 通过泊松过程生成随机到达 C++

python - 如何使用PyTorch打印出每个类别的预测准确率?

Python 样式 : should I avoid commenting my import statements?

python - 参数 1 具有意外类型 'Ui_mainWindow'

java - 如何在Android中生成特定范围内的随机数?

python - 猜数字游戏,无法进行下一次猜测

c# - 生成多个随机数