8
A
回答
34
你可能在你的工作目录中的文件名为random.py或random.pyc。这隐藏了内置的随机模块。您需要将random.py重命名为像my_random.py和/或删除random.pyc文件。
说句肯定发生了什么事情,这样做:
>>> import random
>>> print random.__file__
会告诉你到底是哪文件被导入。
2
我认为你需要提供更多的信息。根据问题中的信息来回答为什么它不工作是不可能的。随机的基本文件在: https://docs.python.org/library/random.html
你可能会在那里检查。
0
你能发表一个你想要做的例子吗?从您的问题中不清楚实际问题是什么。
这里有一个如何使用随机模块的示例:
import random
print random.randint(0,10)
1
Python 2.5.2 (r252:60911, Jun 16 2008, 18:27:58)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed()
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>> random.randint(0,3)
3
>>> random.randint(0,3)
1
>>>
0
似乎为我工作的罚款。看看在official python documentation方法随机:
>>> import random
>>> random.random()
0.69130806168332215
>>> random.uniform(1, 10)
8.8384170917436293
>>> random.randint(1, 10)
4
0
工作对我来说:
Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> brothers = ['larry', 'curly', 'moe']
>>> random.choice(brothers)
'moe'
>>> random.choice(brothers)
'curly'
1
您可能运行的脚本名为random.py本身吗?
3
发生这种情况是因为您在python搜索路径中有一个random.py文件,很可能是当前目录。
Python正在使用sys.path搜索模块,sys.path通常包含标准site-packages之前的当前目录,其中包含预期的random.py。
预计这将在Python 3.0中得到解决,以便您无需使用特殊导入语法即可从当前目录导入模块。
只需从您运行python的目录中删除random.py + random.pyc,它就可以正常工作。
相关问题
- 1. 随机不工作
- 2. Python随机变量不正常工作
- 3. CGPointMake(随机(),随机())不工作
- 4. jQuery随机数不工作
- 5. getLoginUrl随机不工作
- 6. 在IE中不工作的随机div
- 7. 用户Crontab + Python +随机壁纸=不工作?
- 8. 随机生成器,种子不工作
- 9. 随机数和tkinter不工作
- 10. glColor不工作,出现随机颜色
- 11. 生成随机数字不工作
- 12. Xcode的随机变量不工作
- 13. 随机。下一个不工作?
- 14. 随机数插入MySQL的不工作
- 15. 选择随机XML元素 - 不工作
- 16. 随机选择工作不正常
- 17. Solr spellcheckin随机工作
- 18. javascript onclick随机工作
- 19. 随机数如何工作?
- 20. Genymotion随机停止工作
- 21. delayed_job,工作随机消失
- 22. javascript animate-ScrollTop随机工作
- 23. Filesystemwatcher随机停止工作
- 24. Cufon(显然)随机在IE7中工作
- 25. Python - 随机卡
- 26. srand()在随机初始化阵列下工作不起作用
- 27. Java自动提交随机;有时工作,有时不工作
- 28. Python随机数
- 29. CultureInfo.ClearCachedData不起作用。它随机有时有时不工作
- 30. CakePHP 2.5搜索框不工作
在Python 3.3.2 shell中,我必须省略`print`来获取文件路径。 – Tony 2014-10-16 10:13:42