首页 > 其他分享 >Numpy之random.randint产生随机整数

Numpy之random.randint产生随机整数

时间:2023-03-07 13:07:33浏览次数:30  
标签:randint dtype random 整数 high np Numpy

前言

本文主要讲述了如何使用Numpy的random.randint来产生随机整数,我们演示了如何生成不同上限或下限的指定大小的数组

方法
numpy.random.randint(low, high=None, size=None, dtype='l')
返回值

返回从低(包括)到高(不包括)的随机整数。 从“半开”区间 [low, high) 中指定 dtype 的“离散均匀”分布返回随机整数。如果 high 为 None(默认值),则结果来自 [0, low)。

参数

这个方法产生离散均匀分布的整数,这些整数大于等于low,小于high。

  • low : int
    产生随机数的最小值
  • high : int, optional
    给随机数设置个上限,即产生的随机数必须小于high
  • size : int or tuple of ints, optional
    输出的大小,可以是整数,或者元组
  • dtype : dtype, optional
    期望结果的类型
结果
np.random.randint(2, size=10)

array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random

生成 0 到 4 之间的 2 x 4 整数数组

np.random.randint(5, size=(2, 4))

array([[4, 0, 2, 1], # random
[3, 2, 2, 0]])

生成具有 3 个不同上限的 1 x 3 数组

np.random.randint(1, [3, 5, 10])

array([2, 2, 9]) # random

生成具有 3 个不同下限的 1 x 3 数组

np.random.randint([1, 5, 7], 10)

array([9, 8, 7]) # random

使用 dtype 为 uint8 的广播生成 2 x 4 数组

np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)

array([[ 8, 6, 9, 7], # random
[ 1, 16, 9, 12]], dtype=uint8)

实验

Numpy之random.randint产生随机整数_数组

标签:randint,dtype,random,整数,high,np,Numpy
From: https://blog.51cto.com/u_15996214/6105905

相关文章