简体   繁体   中英

How to generate random numbers in a list of lists

I'm here to ask help for a program that I don't know how to resolve:

from random import randint
rain=[]

x=randint(0,500)
y=randint(0,500)

r=2
l=[x,y,r]

for k in range(7):
    rain.append(l)
print(rain)

I just want my program to produce random numbers in the rain list but my code produces:

 [[46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2]]

You can do it this way:

from random import randint

rain = list()

r = 2

for k in range(7):
    x = randint(0, 500)
    y = randint(0, 500)
    rain.append([x, y, r])
print(rain)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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