Python random模块sample、randint、shuffle、choice随机函数概念和应用
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()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间。
random.uniform()正好弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限。
random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,python random.randint。
random.choice()可以从任何序列,比如list列表中,选取一个随机的元素返回,可以用于字符串、列表、元组等。
random.shuffle()如果你想将一个序列中的元素,随机打乱的话可以用这个函数方法。
random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。
使用例子:
# -*- coding: utf-8 -*-
import random print "\n\t"
print "start test choice:"
foo = ['a', 'b', 'c', 'd', 'e']
print random.choice(foo) print "\n\t"
# -*- coding: utf-8 -*-
import random print "\n\t"
print "start test choice:"
foo = ['a', 'b', 'c', 'd', 'e']
print random.choice(foo) print "\n\t"
print "start test slice:"
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回
print slice
print list #原有序列并没有改变 print "\n\t"
print "start test uniform:"
print random.uniform(10, 20)
print random.uniform(20, 10) print "\n\t"
print "start test randint:"
print random.randint(10, 20)
print random.randint(0, 1) print "\n\t"
print "start test random:"
print random.random()*1000
print random.random() print "\n\t"
print "start test shuffle:"
li=range(20)
print random.shuffle(li)
print li
start test choice:
a start test slice:
[1, 4, 10, 8, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] start test uniform:
10.4775179432
10.5882871067 start test randint:
20
0 start test random:
483.103200723
0.430725744563 start test shuffle:
None
[13, 6, 5, 2, 10, 18, 0, 7, 3, 16, 4, 11, 15, 12, 9, 8, 17, 19, 1, 14]
Python random模块sample、randint、shuffle、choice随机函数概念和应用的更多相关文章
- Python random模块sample、randint、shuffle、choice随机函数
一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等. 二.random模块重要函数 1 ).ra ...
- Python random模块 例子
最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random random.random()用于生成一个0到1的随机符点数: ...
- python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET
python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET python random模块 分类: python 2011-11-15 15:31 6037人阅读 评论(2) ...
- python random模块(14)
random 模块包括返回随机数的函数,可以用于模拟或者任何产生随机输出的程序. 一.random模块常用函数介绍 random.random() — 生成一个从0.0(包含)到 1.0(不包含)之间 ...
- python random模块导入及用法
random是程序随机数,很多地方用到,验证码,图片上传的图片名称等,下面说说python random模块导入及用法 1,模块导入 import random 2,random用法 random.r ...
- python random 模块的用法
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- python random模块(随机数)详解
使用前要先导入random模块 import random random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random ...
- Python random模块&string模块 day3
一.random模块的使用: Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. 1.常用函数: (1)random.random() 用于生成一个0到1 ...
- 【python测试开发栈】帮你总结python random模块高频使用方法
随机数据在平时写python脚本时会经常被用到,比如随机生成0和1来控制逻辑.或者从列表中随机选择一个元素(其实抽奖程序也类似,就是从公司所有人中随机选择中奖用户)等等.这篇文章,就帮大家整理在pyt ...
随机推荐
- 51Nod--1008
1008 N的阶乘 mod P 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 % ...
- gitlab环境搭建
企业级的git管理程序.最好用的之一吧.估计其它的也得叫之二. 怎么搭建呢? 2G以上配置的机器才可以搞. . 如下 安装需要注意的是.如果开了80端口,8080的需要修改配置文件 下面会提到 1. ...
- 一些内存模型、并发、netty知识点的记录
happens-before:描述内存可见性as-if-serial:无论怎么重排序,程序的运行结果不会改变 ReentrantLock依赖了队列同步器AQS,其实现方式是volatile变量的读写操 ...
- Scala入门系列(七):面向对象之继承
extends 与Java一样,也是使用extends关键字,使用继承可以有效复用代码 class Person { private var name = "leo" def ge ...
- flask-login使用笔记
看外国文献的中文翻译 翻译的程度有的让人会疯,翻译最好的状态是异译 直译会显的很生硬 看起来确实难过:所以在看的时候,建议都看外国文献吧,或者自己用谷歌翻译,感觉比一些翻译的博客准多了: 在使用fl ...
- 双向bfs-八数码问题
[八数码][1] [1]: https://www.luogu.org/problem/show?pid=1379 其实除了搜索恶心一点,好像也没什么提高+的 bfs搜的是状态. 双向bfs同时从起点 ...
- OBS源码解析(2)run_program函数
static int run_program(fstream &logFile, int argc, char *argv[]){ /*初始化和读取profile相关信息*/ int ret ...
- 一个模型中有两个外键指向同一张表时,创建迁移模型时报错:“ HINT: Add or change a related_name argument to the definition for 'AnswersModel.author' or 'AnswersModel.relay_to'.”解决方案
class AnswersModel(models.Model): author = models.ForeignKey(FrontUserModel,null=True,related_name=' ...
- php两个问号??表示什么意思,PHP两个问号运算符,双问号表达式
有同学给子恒老师留言, 说在php源代码中看到有两个问号?? 不知道是什么意思. 其实两个问题??是php7新推出的表达式, c = a ?? b; 表示如果a非空,则c = a, 如果a为空,则 c ...
- thinkphp5基础
前面的话 ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应用开发和简化企业应用开发而诞生的.ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持 ...