Python随机函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14985798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python random function
提问by donsavage
I'm having problems with Python's import random function. It seems that import randomand from random import randomare importing different things. I am currently using Python 2.7.3
我在使用 Python 的导入随机函数时遇到了问题。似乎import random和from random import random正在进口不同的东西。我目前正在使用 Python 2.7.3
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> random()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
random()
NameError: name 'random' is not defined
>>> random.randint(1,5)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
random.randint(1,5)
NameError: name 'random' is not defined
>>> import random
>>> random()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
random()
TypeError: 'module' object is not callable
>>> random.randint(1,5)
2
>>> from random import random
>>> random()
0.28242411635200193
>>> random.randint(1,5)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
random.randint(1,5)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint'
>>>
采纳答案by jam
import randomimports the random module, which contains a variety of things to do with random number generation. Among these is the random() function, which generates random numbers between 0 and 1.
import random导入 random模块,其中包含与随机数生成有关的各种事情。其中包括 random()函数,它生成 0 到 1 之间的随机数。
Doing the import this way this requires you to use the syntax random.random().
以这种方式进行导入需要您使用语法random.random()。
The random function can also be imported from the module separately:
随机函数也可以单独从模块中导入:
from random import random
This allows you to then just call random()directly.
这使您可以random()直接调用。
回答by Mark Ransom
The problem is that there are two things called random here: one is the module itself, and one is a function within that module. You can't have two things with the same name in your namespace so you have to pick one or the other.
问题是这里有两种叫做 random 的东西:一种是模块本身,一种是该模块中的函数。您的命名空间中不能有两个名称相同的内容,因此您必须选择其中一个。
回答by Andrew Clark
The random modulecontains a function named random(), so you need to be aware of whether you have imported the module into your namespace, or imported functions from the module.
该随机模块包含一个名为功能random(),所以你需要知道你是否已导入模块到您的命名空间,或从模块导入的功能。
import randomwill import the random module whereas from random import randomwill specifically import the random function from the module.
import random将导入 random 模块,而from random import random将从模块中专门导入 random 函数。
So you will be able to do one of the following:
因此,您将能够执行以下操作之一:
import random
a = random.random()
b = random.randint(1, 5)
# you can call any function from the random module using random.<function>
or...
或者...
from random import random, randint # add any other functions you need here
a = random()
b = randint(1, 5)
# those function names from the import statement are added to your namespace
回答by Silas Ray
Well, yes, they import different things. import randomimports the randommodule, from random import randomimports the randomfunction from the randommodule. This is actually a good example of why when designing an API in Python, it's often a good idea to try to avoid naming modules and their members the same thing.
嗯,是的,他们进口不同的东西。 import random导入random模块,从模块from random import random导入random函数random。这实际上是一个很好的例子,说明为什么在用 Python 设计 API 时,尽量避免将模块及其成员命名为同一事物通常是个好主意。
回答by argentage
import random
includes the module into the namespace under the name 'random'.
将模块包含在名称为“random”的命名空间中。
from random import random
includes the function'random' from the namespace 'random' into the global namespace.
将命名空间 'random' 中的函数 'random' 包含到全局命名空间中。
So in the first example, you would call random.random, and in the second, you would call random. Both would access the same function.
因此,在第一个示例中,您将调用 random.random,而在第二个示例中,您将调用 random。两者都将访问相同的功能。
Similarly,
相似地,
from random import randint
would import randint into the global namespace, so you could simply call randint instead of random.randint.
会将 randint 导入全局命名空间,因此您可以简单地调用 randint 而不是 random.randint。
回答by RobH
If you use from random import random, you must call randint() like so: randint(1,5). If you use import random, you call it like so: random.randint(1,5).
如果你使用from random import random,你必须调用randint()像这样:randint(1,5)。如果你使用import random,你可以这样称呼它:random.randint(1,5)。
回答by Adrien Plisson
The 'random' module is a package from the python standard library, as well as a function defined in this package.
'random' 模块是来自 python 标准库的一个包,也是这个包中定义的一个函数。
Using 'import random' imports the package, which you can then use the function from this package: 'random.random()'. You can use any other function from the 'random' package as well.
使用 'import random' 导入包,然后您可以使用该包中的函数:'random.random()'。您也可以使用 'random' 包中的任何其他函数。
You can also tell python to specifically import only the random function from the package random: 'from random import random'. Then you can only use the function 'random()', and should not specify the package it comes from. However you cannot use any other function from the random package, because they have not been imported if you use 'from random import random'.
您还可以告诉 python 只从包 random: 'from random import random' 中专门导入随机函数。那么你只能使用函数'random()',并且不应该指定它来自的包。但是,您不能使用 random 包中的任何其他函数,因为如果您使用“from random import random”,它们还没有被导入。
回答by comodoro
If you are using PyDev or other clever IDE, make sure it did not import the module automatically, overriding your import. It can be especially confusing here, when module name is equal to a function name, because the error thrown is not a NameError. In my case I added
如果您使用 PyDev 或其他聪明的 IDE,请确保它没有自动导入模块,覆盖您的导入。当模块名称等于函数名称时,这里可能特别令人困惑,因为抛出的错误不是NameError. 就我而言,我添加了
import random
and later used it:
后来使用了它:
r = random.radom()
but got:
但得到:
AttributeError: 'builtin_function_or_method' object has no attribute 'random'
Only after searching I found that PyDev automatically added the line
搜索后才发现PyDev自动添加了这一行
from random import random
to the end of my imports, so I was in fact calling attribute randomof method random. Solution is to delete the automatic import or use it and call the random()method directly.
到我的导入结束,所以我实际上是在调用randommethod 的属性random。解决办法是删除自动导入或者random()直接使用并调用方法。
回答by Death
You have to import the random function, from the random modulebefore you can use it use it
您必须先从 random模块中导入 random函数,然后才能使用它
In [1]: from random import random
In [2]: random()
Out[2]: 0.5607917948041573

