使用Python中的numpy库,使用np.random.rand()方法来生成随机数,并将它们分配给不同的列。
示例代码如下:
import numpy as np
arr = np.zeros((4, 4))
arr[:, 0] = np.random.rand(4) arr[:, 1] = np.random.rand(4) arr[:, 2] = np.random.rand(4) arr[:, 3] = np.random.rand(4)
print(arr)
运行代码后,将生成如下所示的4x4数组:
[[0.03519243 0.87556289 0.84526317 0.11004969] [0.80911047 0.75477316 0.13268441 0.84074494] [0.15618036 0.06870231 0.70568603 0.52452017] [0.39196036 0.9122358 0.57311894 0.32921994]]
每一列都分别生成了0到1之间的随机数。
上一篇:不同列中的值计数器
下一篇:不同列中数值比例的条形图