How do i get 10 random values in python?

I am new to programming and I got stuck with random number generation. I can simply generate random numbers using random function "randint" but could not generate set of random numbers. For instance i want to get 10 random numbers.

from random import randint
x = randint(1, 100)
y = randint(1, 100)
isFailedTest = (5<=x<=15) and (10<=y<=11)
selected_test = [x,y]
while (isFailedTest == False):

I can generate 1 random number at one time but not 10 at one time. Here 1 number mean 2 dimensional number example (x,y) = (10,20) I want to get 10 random numbers (x,y) after my while condition. How do I achieve that? I am very new to programming so could not figure out what could be done. All help/ suggestion/ recommendation is highly appreciated.Thank you.


要求 - “这里1个数字表示二维数字例子(x,y)=(10,20)我想得到10个随机数(x,y)”

>>> from random import randint as r
>>> array = [ (r(1,100), r(1,100)) for i in xrange(10)]

Simple solution

array = [(randint(1, 100), randint(1, 100)) for i in range(10)]

Better solution

The following solution is more flexible and reusable.

from functools import partial
from random import randint


def randints(count, *randint_args):
    ri = partial(randint, *randint_args)
    return [(ri(), ri()) for _ in range(count)]


print(randints(10, 1, 100))

from random import randint
r = []
N = 10
for x in range(N):
    a = randint(5,15)   # rand number between 5 and 15
    b = randint(10,11)  # rand number between 10 and 11
    r.append((a,b))

# r <-- contains N tuples with random numbers
链接地址: http://www.djcxy.com/p/69952.html

上一篇: numpy数组字典列表,不包含for循环

下一篇: 我如何在Python中获得10个随机值?