【发布时间】:2018-12-23 12:31:38
【问题描述】:
我希望打印一个矩阵,其中包含随机列 (0, 9) 和随机行 (0, 9) 以及随机元素 (0, 9)
其中 (0, 9) 是 0 到 9 之间的任意随机数。
【问题讨论】:
-
你能指定一个想要的输出吗?我不确定我是否理解。
标签: python numpy matrix random
我希望打印一个矩阵,其中包含随机列 (0, 9) 和随机行 (0, 9) 以及随机元素 (0, 9)
其中 (0, 9) 是 0 到 9 之间的任意随机数。
【问题讨论】:
标签: python numpy matrix random
如果您要查找的是一个 10x10 矩阵,其中填充了 0 到 9 之间的随机数,那么这就是您想要的:
# this randomizes the size of the matrix.
rows, cols = np.random.randint(9, size=(2))
# this prints a matrix filled with random numbers, with the given size.
print(np.random.randint(9, size=(rows, cols)))
输出:
[[1 7 1 4 4 4 4 3]
[1 4 7 3 0 5 3 5]
[6 3 3 7 5 7 6 1]
[3 8 5 7 2 0 1 6]
[5 0 8 5 0 1 5 1]
[1 3 3 7 3 7 5 6]
[3 7 4 1 8 3 7 8]
[8 8 8 5 8 4 7 1]]
【讨论】:
4s,但other numbers don't seem so random... ;)
首先,随机化列数和行数:
import numpy as np
rows, cols = np.random.randint(10, size = 2)
如果您想要一个整数矩阵,请尝试:
m = np.random.randint(10, size = (rows,cols))
这将输出一个 rows x cols 矩阵,其中包含紧密区间 [0,9] 中的随机数。
如果您想要一个浮点数矩阵,请尝试:
m = np.random.rand(rows,cols) * 9
这将输出一个 rows x cols 矩阵,其中包含紧密区间 [0,9] 中的随机数。
【讨论】: