栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python np.random.randint() 参数 详解 用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python np.random.randint() 参数 详解 用法



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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/998347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号