Python使用numpy.random.randint产生随机整数矩阵

原型:numpy.random.randint(low, high=None, size=None, dtype='1')      这个方法产生离散均匀分布的整数,这些整数大于等于low,小于high。
参数:

low : int   #产生随机数的最小值

high : int, optional   #给随机数设置个上限,即产生的随机数必须小于high

size : int or tuple of ints, optional   #输出的大小,可以是整数,或者元组

dtype : dtype, optional   #期望结果的类型

numpy.random.randint的low、high、size三个参数。默认high是None,如果只有low,那范围就是[0,low)。如果有high,范围就是[low,high)。

实例如下:

#conding:utf-8

import numpy as np
import random
w = np.random.randint(2,size=(2, 3))      #产生2行3列的0,1矩阵
print("w-----------")

print(w)

w1 = np.random.randint(5, size=8)         
print("w1-----------")

print(w1)

输出为:

w-----------

[[1 0 0]

 [0 0 0]]

w1-----------

 [0 4 4 0 1 4 2 1]

你可能感兴趣的:(学习)