np.random.randint() 根据参数中所指定的范围生成随机 整数。
numpy.random.randint(low, high=None, size=None, dtype=int)
参数 1. low: int 生成的数值的最小值(包含),默认为0,可省略。 2. high: int 生成的数值的最打值(不包含)。 3. size: int or tuple of ints 随机数的尺寸, 默认是返回单个,输入 10 返回 10个,输入 (3,4) 返回的是一个 3*4 的二维数组。(可选)。 4. dtype:想要输出的结果类型。默认值为int。(可选,一般用不上)。
一、基础用法
import pandas as pd import numpy as np # 随机返回 0-9 的 一个整数 np.random.randint(10) # 随机返回 10-20 的 一个整数 np.random.randint(10,21) # 随机返回 10-20 的 长度为 10 的数组 np.random.randint(10,21,size=10) # 随机返回 0-9 的 3*4 随机数组 np.random.randint(10,size=(3,4)) # 随机返回 10-20 的 3*4 随机数组 np.random.randint(10,21,size=(3,4))
二、高级用法
import pandas as pd import numpy as np # 高级用法 # 生成 3 个最大值分别为 3,5,7 的值,最大值不超过 10 的值 np.random.randint([3, 5, 7], 10) # 高级用法 # 生成 3 个最小值为1,最大值分别不超过 3,5,10 的值 np.random.randint(1, [3, 5, 10]) # 高级用法 # 生成 4*2 数组 最小值为[1, 3, 5, 7],最大值 第一行不超过10,第二行不超过 20 np.random.randint([1, 3, 5, 7], [[10], [20]]) # 随机返回 10-20 的 长度为 10 dtype=np.uint8 的数组 np.random.randint(10,21,size=10,dtype=np.uint8)



