编写一个Python函数,生成一个m行n列的元素值不大于20的随机数矩阵(二维列表)。
提示:使用random.randint()函数来生成随机数。
函数接口定义:
def generateMatrix(m,n)
m: 行数,int, 1 < m < 100; n: 列数, int, 1 < n < 100.
裁判测试程序样例:
import random
m = int(input())
n = int(input())
r = generateMatrix(m,n)
p = True
for i in range(m):
for j in range(n):
if r[i][j] > 20:
p = False
break
if m > 3 and n > 3:
if r[0][0] == r[2][2] and r[1][1] == r[1][2] and r[0][2] == r[3][0]:
p = False
print§
#测试程序的输出:
True
def generateMatrix(m,n):
la=[]
for j in range(m):
la.append([0]*n)
for i in range(m):
for j in range(n):
la[i][j]=random.randint(-99,21)
return la