Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 导入随机isn';(我想)不工作_Python_Random - Fatal编程技术网

Python 导入随机isn';(我想)不工作

Python 导入随机isn';(我想)不工作,python,random,Python,Random,我是Python的新手(大学一年级的学生),在试图完成一些家庭作业时,我遇到了一个问题。我正在用随机数创建一个非常简单的程序。以下是我到目前为止所写的内容: import random def main(): print random.randint(2,10) a=random.seed() b=random.seed(5) c=random.seed(5) print (a) print (b) print (c) 问题是,它总是不为a、b和c打印任何内容。对

我是Python的新手(大学一年级的学生),在试图完成一些家庭作业时,我遇到了一个问题。我正在用随机数创建一个非常简单的程序。以下是我到目前为止所写的内容:

import random

def main():
  print random.randint(2,10)
  a=random.seed()
  b=random.seed(5)
  c=random.seed(5)
  print (a)
  print (b)
  print (c)
问题是,它总是不为a、b和c打印任何内容。对于random.int,它总是打印我输入的最小整数(本例中为2)。所以这基本上使我无法编写实际的程序(一个随机的糖果分发器)。我实际上只是假设“随机导入”不起作用。我其实不知道


我通常会问我的教授这件事,但他要到星期一才能联系到,而且这项作业今晚就要交了。非常感谢您的帮助,我为这篇文章的业余性道歉。

这是因为
random.seed
只为随机数生成器重新设定种子。它本身不会返回任何内容(请参阅)。这意味着它返回
None
,这是您在测试中看到的结果。

您首先应该设置种子值,这是一个稍后“确定”随机值的值,通常在开始(或首次使用随机数)期间只需要设置一次

这主要是基于当前时间

然后您可以连续呼叫以获取随机号码:

print random.randint(2,10)

你可以通过打字看到

>>> import random
>>> help(random.seed)
random.random实例的种子(a=None,version=2)方法 从哈希对象初始化内部状态

None or no argument seeds from current time or from an operating
system specific randomness source if available.

For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray.  For version 1, the hash() of *a* is used instead.

If *a* is an int, all bits are used.
random.seed
用于设置随机生成器

为了获得值,您需要使用
random
模块提供的众多函数之一

e、 g


random.seed()
不返回任何内容。对其进行测试,方法是对范围内的numb(100)执行
然后在其下打印(random.randint(1,10))是否阅读了
random.seed
的文档?我想你误解了它的作用。非常感谢。我现在觉得自己像个白痴。我肯定把它和别的东西搞混了。永远不要觉得自己像个白痴,只是说。我们都是来学习的!祝你们两位回答者度过愉快的一天;还有未来//威廉
None or no argument seeds from current time or from an operating
system specific randomness source if available.

For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray.  For version 1, the hash() of *a* is used instead.

If *a* is an int, all bits are used.
import random

def main():
  print random.randint(2,10)
  a=random.seed()
  b=random.randrange(5)
  c=random.randrange(5)
  print (a)
  print (b)
  print (c)