Numpy之random.randint产生随机整数

您所在的位置:网站首页 python怎么输出一个整数 Numpy之random.randint产生随机整数

Numpy之random.randint产生随机整数

2024-07-15 14:41| 来源: 网络整理| 查看: 265

前言

本文主要讲述了如何使用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 给随机数设置个上限,即产生的随机数必须小于highsize : 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)

实验

这里写图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3