Python random 模块详解 sample、randint、shuffle、choice 随机函数
来源:互联网 发布:阿里云cdn流量是什么 编辑:程序博客网 时间:2024/06/06 08:42
一、random模块简介
Python标准库中的random函数,可以生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等。
二、random模块重要函数
1 )、random() 返回0<=n<1之间的随机实数n;
2 )、choice(seq) 从序列seq中返回随机的元素;
3 )、getrandbits(n) 以长整型形式返回n个随机位;
4 )、shuffle(seq[, random]) 原地指定seq序列;
5 )、sample(seq, n) 从序列seq中选择n个随机且独立的元素;
三、random模块方法说明
random.random()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间。
random.uniform()正好弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限。
random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,python random.randint。
random.choice()可以从任何序列,比如list列表中,选取一个随机的元素返回,可以用于字符串、列表、元组等。
random.shuffle()如果你想将一个序列中的元素,随机打乱的话可以用这个函数方法。
random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。
阅读全文
0 0
- Python random 模块详解 sample、randint、shuffle、choice 随机函数
- 【Python】区分python中random模块的randint与numpy.random模块的randint
- python random.shuffle(随机打乱列表等) 和 random.random
- python中np.random.randint()
- Python-- 有权重的随机选择, Weighted Random Choice
- python.matplotlib/random.choice学习范例-随机生成点图
- Numpy之random.randint产生随机整数
- random.randint
- python中的随机函数random
- Python的numpy库中rand(),randn(),randint(),random_integers()等random系函数的使用
- Python之随机生成数random模块
- Python random模块(随机整数部分内容)
- numpy的random.choice函数
- numpy函数之random.choice
- [numpy]random.choice()随机选取内容
- python 随机数模块random详解
- Python random模块常用函数
- python random.shuffle()
- 19个PLSQL实例
- NOIP2015 子串 【动态规划】
- leetcode5 Longest Palindromic Substring
- Hibernate基础加强
- java 发送短信demo
- Python random 模块详解 sample、randint、shuffle、choice 随机函数
- Pandas与Numpy中axis参数的二义性
- 用PyTorch实现一个卷积神经网络进行图像分类
- 【C/C++】如何理解C/C++里的动态内存和资源管理
- myeclipse 自定义视图Customize Perspective 没有反应
- 非常可乐 数学推导
- HTTP与HTTPS的区别
- PROBLEM:YBT1334
- 2017年11月01日普及组 I Liked Matrix!