[英]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 is supposed to give random numbers though os.urandom
, randint
works like normal randrange
: SystemRandom 应该通过
os.urandom
给出随机数,而randint
的工作方式与普通randrange
一样:
>>> print(random.SystemRandom.randint.__doc__)
Return random integer in range [a, b], including both end points.
In IDLE a little popup suggestion appears when I type it it saying在 IDLE 中,当我输入它时会出现一个小的弹出建议
`random.SystemRandom.randint(self, a, b)`
I think this is the cause.我认为这是原因。 I'm not very good at using classes and understanding how they work, but the first argument seems to be being passed as
self
, when it should be a
.我不是很擅长使用类和理解它们是如何工作的,但是第一个参数似乎是作为
self
传递的,而它应该a
. I never really understand why self
is used when it isn't even a keyword, and how it's supposed to all just work, but it generally does.我从来没有真正理解为什么在它甚至不是关键字时使用
self
,以及它应该如何正常工作,但它通常会起作用。
Am I doing this wrong, or should I report this to the Python Foundation whenever one is supposed to do such things?我做错了吗,或者我应该在应该做这样的事情时向 Python 基金会报告吗?
random.SystemRandom
is an class. random.SystemRandom
是一个类。 It needs to be instantiated;它需要被实例化;
In [5]: foo = random.SystemRandom()
In [6]: foo.randint(0, 10)
Out[6]: 0
The complete docstring gives a hint;完整的文档字符串给出了提示;
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
The self
parameter indicates that this randint
is a method of SystemRandom
.的
self
参数表明,该randint
是的方法SystemRandom
。
I think you want:我想你想要:
import random
r = random.SystemRandom()
print(r.randint(0, 10))
instead.反而。
This is because you need to create an instance of the random.SystemRandom
class.这是因为您需要创建
random.SystemRandom
类的实例。
However you can just as easily use the following:但是,您可以轻松地使用以下内容:
import random
random.randint(0, 10)
if you don't need an OS-dependent cryptographic RNG.如果您不需要依赖于操作系统的加密 RNG。
When the error is " TypeError: randint() missing 1 required positional argument: 'b' " it's because the person writing the code probably mixed up their当错误是“ TypeError: randint() missing 1 required positional argument: 'b' ”时,这是因为编写代码的人可能混淆了他们的
random.randint()
and their和他们的
random.choice()
Hope I helped :D希望我有所帮助:D
May be while using Jupyter notebook you got an error like this,Just pass the missing positional argument like randint(1,1000) it will generate a random number bw 1 to 1000.Or Re-run the random module then try to execute both will work fine可能在使用 Jupyter notebook 时出现这样的错误,只需传递缺少的位置参数,如 randint(1,1000),它将生成一个 1 到 1000 之间的随机数。或者重新运行随机模块,然后尝试执行两者工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.