1. python中的随机函数
描述:random() 方法返回随机生成的一个实数,它在[0,1)范围内。
import random random.random();
>>> import random >>> print random.random(); 0.803119901575 >>> print random.random(); 0.451592468747
语法
import random
random.randrange ([start,] stop [,step])
#参数:
start -- 指定范围内的开始值,包含在范围内
stop -- 指定范围内的结束值,不包含在范围内。
step -- 指定递增基数
实例演示
>>> print random.randrange(10); 4 >>> print random.randrange(5,10); 7 >>> print random.randrange(5,10,3); 5 >>> print random.randrange(5,10,3); 8
import random
random.randint(x,y)
#参数:
x -- 指定范围内的开始值,包含在范围内
y -- 指定范围内的结束值,包含在范围内。
实例演示
>>> print random.randrange(5,10); 9 >>> print random.randint(5,10); 6
语法:
import random
random.uniform (x,y)
#参数:
x -- 指定范围内的开始值,包含在范围内
y -- 指定范围内的结束值,包含在范围内。
实例演示
>>> print random.uniform(5,10); 9.13282585434 >>> print random.uniform(9,10); 9.95958315062
语法
import random
random.choice(x)
#参数:
x -- list,tuple,strings的一种
实例演示
>>> print random.choice(('a','be',5,'e'))
5
>>> print random.choice([10,2,6,5,85,'af'])
85
>>> print random.choice('i love python')
v
6. sample()函数
语法
import random
random.sample(x,n)
#参数:
x -- list,tuple,strings的一种
n -- 返回n个随机项
实例演示
>>> print random.sample('i love python',3)
[' ', 'e', 'i']
>>> print random.sample([10,20,50,23,'ab'],3)
[50, 'ab', 23]
>>> print random.sample((10,20,50,23,'ab'),3)
[50, 20, 'ab']
描述:shuffle() 方法将序列的所有元素随机排序。类似于洗牌
import random
random.shuffle(x)
#参数:
x -- list,tuple的一种;python2.x只支持list类型
实例演示
>>> list=['a','b','c','d','e']; >>> random.shuffle(list); >>> print list; ['c', 'd', 'a', 'e', 'b']
拓展:将元祖反转;实现reverse函数的效果
>>> list=['a','b','c','d','e']; >>> list1=list[::-1] >>> print list1 ['e', 'd', 'c', 'b', 'a']
1. python中的随机函数的更多相关文章
- python中的随机函数random的用法示例
python中的随机函数random的用法示例 一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据 ...
- python中的随机函数
python--随机函数(random,uniform,randint,randrange,shuffle,sample) 本文转载自:[chamie] random() random()方法:返回随 ...
- [转]Python中的str与unicode处理方法
早上被python的编码搞得抓耳挠腮,在搜资料的时候感觉这篇博文很不错,所以收藏在此. python2.x中处理中文,是一件头疼的事情.网上写这方面的文章,测次不齐,而且都会有点错误,所以在这里打算自 ...
- python中的Ellipsis
...在python中居然是个常量 print(...) # Ellipsis 看别人怎么装逼 https://www.keakon.net/2014/12/05/Python%E8%A3%85%E9 ...
- python中的默认参数
https://eastlakeside.gitbooks.io/interpy-zh/content/Mutation/ 看下面的代码 def add_to(num, target=[]): tar ...
- Python中的类、对象、继承
类 Python中,类的命名使用帕斯卡命名方式,即首字母大写. Python中定义类的方式如下: class 类名([父类名[,父类名[,...]]]): pass 省略父类名表示该类直接继承自obj ...
- python中的TypeError错误解决办法
新手在学习python时候,会遇到很多的坑,下面来具体说说其中一个. 在使用python编写面向对象的程序时,新手可能遇到TypeError: this constructor takes no ar ...
- python中的迭代、生成器等等
本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...
- python2.7高级编程 笔记二(Python中的描述符)
Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...
随机推荐
- [转载]Oracle修改表空间大小
Oracle修改表空间大小 使用Oracle10g建立数据库后,向数据库中导入了部分数据,第二天继续向数据库中导入数据表时发生错误: 查了很多资料发现原来是Oracle表空间限制,导致无法继续导入数据 ...
- 分享书籍[writing idiomatic python ebook]
你是不是总是觉得学了python好久,蓦然回首,总是感觉写的代码不是那么有pythonic的味道.看看别人的代码(django,webpy),再看看自己的代码,觉得就是一java-python的混合体 ...
- ARC 没有自动释放内存
http://www.cnblogs.com/qingche/p/4569833.html 定位了好几天,才发现是打印日志没有即时释放内存,使用intrustment
- 网卡流量查看软件bmon
bmon 时 linux下最常用的查看网络带宽的工具,debian下直接进行安装即可 apt-get install bmon redhat下可以在这里寻找到合适版本的rpm包,安装完毕后执行bmon ...
- expect实现交互式脚本
#!/usr/bin/expect -f ##告诉解释器用expect来解释 set timeout 6 ##设置超时时间 ] ## 这个是传递给脚本的第一个参数,并把参数赋值给user ] ## 这 ...
- RAC数据库迁移ASM磁盘组到其它存储
环境介绍: 一共有两个磁盘组:crs和data:crs使用normal冗余:data使用外部冗余. 添加新的asm磁盘过程(略) 1.迁移前 SQL> select group_number, ...
- WEB urllib2 module note
收藏好文,看的懂文档,但效率太慢 cookie 清空 import urllib2 import cookielib from time import sleep cookie=cookielib.C ...
- OLTP与OLAP的差异
OLTP与OLAP的差异 系统类型 OLTP(在线交易系统) OLAP(联机分析系统),DW(数据仓库) 数据来源 操作数据,OLTP通常是原始性数据源 联合型数据:OLAP数据来源于其他OLTP系统 ...
- Leetcode: Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- angular 依赖注入
依赖注入 依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...