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).对于大部分特性来说,这些" ...
随机推荐
- javascript实例学习之六—百叶窗效果
一.要结合布局才能形成百叶窗的效果 二.开启两个定时器,第一个定时器控制的是百叶窗整体一次上翻,一次下翻:另一个控制百叶窗中的各页逐次翻转,形成层次效果.否则就成了普通的滚动广告的效果了 本实现借助于 ...
- Array.prototype.slice && Array.prototype.splice 用法阐述
目的 对于这两个数组操作接口,由于不理解, 往往被误用, 或者不知道如何使用.本文尝试给出容易理解的阐述. 数组 什么是数组? 数组是一个基本的数据结构, 是一个在内存中依照线性方式组织元素的方式, ...
- Oracle Flashback Technologies - 闪回被drop的表
Oracle Flashback Technologies - 闪回被drop的表 在oracle10g中,drop一个表后,表没有真正被删除,支持被rename后放在recyclebin中. #新建 ...
- iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“
需要你写一下你确认提交的信息,就是你这次提交上去修改了什么功能,简单描述一下
- :“boost/serialization/string.hpp”: No such file or directory 错误
主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html
- 转:python webdriver API 之cookie 处理
有时候我们需要验证浏览器中是否存在某个 cookie,因为基于真实的 cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除 cookie 信息.webdrive ...
- Java基础(31):String的大小写转换、分离成数组、==与equals()的区别(String类)
继续来看 String 类常用的方法,如下代码所示: 运行结果: 那么,“==” 和 equals() 有什么区别呢? ==: 判断两个字符串在内存中首地址是否相同,即判断是否是同一个字符串对象 eq ...
- WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedExcepti ...
- Openssl生成根证书、服务器证书并签核证书
1.修改Openssl配置文件CA目录: cat /etc/pki/tls/openssl.cnf dir = /etc/pki/CA 2.生成根证书及私钥: #http://www.haiyun.m ...
- 查看在线EBS用户的相关信息
--查看在线EBS用户的相关信息 SELECT PAP.FULL_NAME, FU.USER_NAME, FAT.APPLICATION_NAME, FRT.RESPONSIBILITY_NAME, ...