简体   繁体   English

使用Random.random [30,35]打印30,35之间的随机数。 种子(70)

[英]Use Random.random [30,35] to print random numbers between 30,35. Seed(70)

Probably a simple answer, not sure what I am missing. 可能是一个简单的答案,不确定我缺少什么。 For a homework assignment I have to use random.random() to generate numbers between 30 and 35. The seed has to be set to 70 to match the pseudo-random numbers with the grader. 对于家庭作业,我必须使用random.random()来生成介于30到35之间的数字。必须将种子设置为70以使伪随机数与评分器相匹配。 This wasn't in my lecture so I am a little stumped as to what to do. 这不是我的演讲,所以我对要做的事情有些困惑。

I have: 我有:

import random 
def problem2_4():
    print(random.random(30,35))

But this is clearly wrong. 但这显然是错误的。

The assignment says the output should look like (note: for the problem i use def problem2_4() just for the assignment grading system) 作业说输出应该看起来像这样(注意:对于这个问题,我仅将def problem2_4()用于作业评分系统)

problem2_4()
[34.54884618961936, 31.470395203793395, 32.297169396656095, 30.681793552717807,
 34.97530360173135, 30.773219981037737, 33.36969776732032, 32.990127772708405, 
 33.57311858494461, 32.052629620057274]

The output [blah, blah, blah] indicates that it is a list of numbers rather than a series of numbers printed one-by-one. 输出[blah, blah, blah]表示它是一个数字列表 ,而不是一个一一打印的一系列数字。

In addition, if you want random floating point values, you'll need to transform the numbers from random.random (which are zero to one) into that range. 另外,如果要使用随机浮点值,则需要将数字从random.random (从零到一)转换为该范围。

That means you'll probably need something like: 这意味着您可能需要类似以下内容:

import random                                # Need this module.
def problem2_4():
    random.seed(70)                          # Set initial seed.
    nums = []                                # Start with empty list.
    for _ in range(10):                      # Will add ten values.
        nums += [random.random() * 5 + 30]   # Add one value in desired range.
    print(nums)                              # Print resultant list.

Of course, the Pythonic way to do this would be: 当然, Pythonic的方法是:

import random
random.seed(70)
print([random.random() * 5 + 30 for _ in range(10)])

Bit that might be a bit ahead of where your educator is working. 这可能比您的教育工作者的工作位置领先。 Still, it's good to learn this stuff as early as possile since you'll never be a Pythonista until you do :-) 尽管如此,还是要尽早学习这些东西,这是一件好事,因为除非您这样做,否则您永远都不是Pythonista了:

The function that you are looking for is randint , which returns an integer (whole number) between the specified minimum and maximum values. 您要查找的函数是randint ,它返回指定的最小值和最大值之间的整数(整数)。

So the solution would be 所以解决方案是

random.randint(30, 35)

Python Docs Python文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM