Python:将随机数放入列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16655089/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:17:07  来源:igfitidea点击:

Python: Random numbers into a list

pythonlistrandom

提问by Linda Leang

Create a 'list' called my_randoms of 10 random numbers between 0 and 100.

创建一个名为 my_randoms 的“列表”,其中包含 0 到 100 之间的 10 个随机数。

This is what I have so far:

这是我到目前为止:

import random
my_randoms=[]
for i in range (10):
    my_randoms.append(random.randrange(1, 101, 1))
    print (my_randoms)

Unfortunately Python's output is this:

不幸的是,Python 的输出是这样的:

[34]
[34, 30]
[34, 30, 75]
[34, 30, 75, 27]
[34, 30, 75, 27, 8]
[34, 30, 75, 27, 8, 58]
[34, 30, 75, 27, 8, 58, 10]
[34, 30, 75, 27, 8, 58, 10, 1]
[34, 30, 75, 27, 8, 58, 10, 1, 59]
[34, 30, 75, 27, 8, 58, 10, 1, 59, 25]

It generates the 10 numbers like I ask it to, but it generates it one at a time. What am I doing wrong?

它像我要求的那样生成 10 个数字,但它一次生成一个。我究竟做错了什么?

回答by karthikr

Fix the indentation of the printstatement

修复print语句的缩进

import random

my_randoms=[]
for i in range (10):
    my_randoms.append(random.randrange(1,101,1))

print (my_randoms)

回答by mattexx

import random
my_randoms = [random.randrange(1, 101, 1) for _ in range(10)]

回答by robertklep

You could use random.sampleto generate the list with one call:

您可以使用random.sample一次调用生成列表:

import random
my_randoms = random.sample(xrange(100), 10)

That generates numbers in the (inclusive) range from 0 to 99. If you want 1 to 100, you could use this (thanks to @martineau for pointing out my convoluted solution):

这会生成 0 到 99(含)范围内的数字。如果你想要 1 到 100,你可以使用这个(感谢@martineau 指出我的复杂解决方案):

my_randoms = random.sample(xrange(1, 101), 10)

回答by perwaiz alam

import random

a=[]
n=int(input("Enter number of elements:"))

for j in range(n):
       a.append(random.randint(1,20))

print('Randomised list is: ',a)

回答by someguy

my_randoms = [randint(n1,n2) for x in range(listsize)]

回答by Stryker

Here I use the samplemethod to generate 10 random numbers between 0 and 100.

这里我使用sample方法生成0到100之间的10个随机数。

Note: I'm using Python 3's rangefunction (not xrange).

注意:我使用的是 Python 3 的range函数(不是xrange)。

import random

print(random.sample(range(0, 100), 10))

The output is placed into a list:

输出被放入一个列表中

[11, 72, 64, 65, 16, 94, 29, 79, 76, 27]

回答by Ben Solien

This is way late but in-case someone finds this helpful.

这已经很晚了,但以防万一有人发现这有帮助。

You could use list comprehension.

您可以使用列表理解。

rand = [random.randint(0, 100) for x in range(1, 11)]
print(rand)

Output:

输出:

[974, 440, 305, 102, 822, 128, 205, 362, 948, 751]

Cheers!

干杯!

回答by vestland

xrange()will not work for 3.x.

xrange()不适用于 3.x。

numpy.random.randint().tolist()is a great alternative for integers in a specified interval:

numpy.random.randint().tolist()是指定区间内整数的一个很好的替代方案:

 #[In]:
import numpy as np
np.random.seed(123) #option for reproducibility
np.random.randint(low=0, high=100, size=10).tolist()

#[Out:]

[66, 92, 98, 17, 83, 57, 86, 97, 96, 47]

You also have np.random.uniform()for floats:

你也有np.random.uniform()花车:

#[In]:
np.random.uniform(low=0, high=100, size=10).tolist()

#[Out]:
[69.64691855978616,
28.613933495037948,
22.68514535642031,
55.13147690828912,
71.94689697855631,
42.3106460124461,
98.07641983846155,
68.48297385848633,
48.09319014843609,
39.211751819415056]