📜  如何在Python中创建随机整数矩阵?

📅  最后修改于: 2022-05-13 01:55:16.952000             🧑  作者: Mango

如何在Python中创建随机整数矩阵?

先决条件: numpy

要在Python中创建随机整数矩阵,请使用 numpy 模块的 randint()函数。此函数用于随机抽样,即所有生成的数字都是随机的,无法预测。

示例 1:

Python3
# importing numpy library
import numpy as np  
  
# random is a function, doing random sampling in numpy.
array = np.random.randint(10, size=(20))
  
# the array will be having 20 elements.
print(array)


Python3
import numpy as np
  
# 1st argument --> numbers ranging from 0 to 9, 
# 2nd argument, row = 2, col = 3
array = np.random.randint(10, size=(2, 3))
print(array)


Python3
import numpy as np
  
  
array = np.random.randint(2, size=(5, 5))
print(array)


输出:

示例 2:

蟒蛇3

import numpy as np
  
# 1st argument --> numbers ranging from 0 to 9, 
# 2nd argument, row = 2, col = 3
array = np.random.randint(10, size=(2, 3))
print(array)

输出:

[[8 6 7]
 [2 9 9]]

示例 3:

蟒蛇3

import numpy as np
  
  
array = np.random.randint(2, size=(5, 5))
print(array)

输出: