简体   繁体   English

是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 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.

相关问题 为什么我使用 random.randint 生成相同的数字? - Why am I generating the same number using random.randint? 在 python 中,当我尝试选择随机数时,如何确保使用 randint 的种子不断变化? - In python How to ensure that seed in using randint keeps changing when i am trying to pick a random number? 除了在索引中使用random.randint()之外,如何从python列表中选择随机字符串? - How do i choose random string from python list other than using random.randint() in index? 在python中使用random.randint帮助 - Using random.randint help in python 使用random.randint()从列表中按索引绘制元素时出现索引错误 - Index error when drawing elements by index from list using random.randint()'s 我正在尝试使用 random.randint 打印列表的随机索引,但我让列表索引超出范围 - I am trying to print a random index of the list using random.randint, but Im getting list index out of range random.randint 错误 - random.randint error Python 随机模块 - randint 错误不确定错误是什么 - Python random module - randint error unsure what the error is python随机模块中的randint()错误&#39;randrange的空范围&#39; - randint() error 'empty range for randrange' in the python random module 在python中使用random.randint从列表中随机选择 - randomly choose from list using random.randint in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM