python---random模块详解
在python中用于生成随机数的模块是random,在使用前需要import, 下面看下它的用法。
random.random
random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
注意: 以下代码在Python3.5下测试通过, python2版本可稍加修改
描述
random() 方法返回随机生成的一个实数,它在(0,1)范围内。
语法
以下是 random() 方法的语法:
import random
random.random()
注意:random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
参数
无
返回值
返回随机生成的一个实数,它在[0,1)范围内。
实例
以下展示了使用 random() 方法的实例:
1
2
3
4
5
6
|
#!/usr/bin/python import random # 生成第一个随机数 print ( "random 1 : " , random.random()) # 生成第二个随机数 print ( "random 2 : " , random.random()) |
以上实例运行后输出结果为:
1
2
|
random 1 : 0.3558774735558118 random 2 : 0.46006891154492147 |
random.uniform
random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: b <= n <= a。如果 a <b, 则 a <= n <= b。
1
2
3
|
import random print (random.uniform( 1 , 10 )) print (random.uniform( 10 , 1 )) |
结果:
1
2
|
2.1520386126536115 3.139127274753873 |
random.randint
random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b,
注意: 下限必须小于上限
1
2
3
|
import random print (random.randint( 11 , 20 )) #生成的随机数n: 12 <= n <= 20 print (random.randint( 20 , 20 )) #结果永远是20 |
结果:
1
2
|
11 20 |
random.randrange
random.randrange的函数原型为:random.randrange([start], stop[, step]),从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
1
2
|
import random print (random.randrange( 10 , 18 , 2 )) |
结果:
1
|
[10, 12, 14, 16, 18] |
random.choice
random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章
1
2
3
4
|
import random print (random.choice( "Pythontab.com" )) print (random.choice([ "python" , "tab" , "com" ])) print (random.choice(( "python" , "tab" , "com" ))) |
结果:
1
2
3
|
t python tab |
random.shuffle
random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱。如:
1
2
3
4
|
import random list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] random.shuffle( list ) print ( list ) |
结果:
1
|
[4, 1, 9, 3, 2, 7, 10, 6, 8, 5] |
random.sample
random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
1
2
3
4
5
|
import random list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] slice = random.sample( list , 5 ) #从list中随机获取5个元素,作为一个片断返回 print ( slice ) print ( list ) #原有序列不会改变。 |
结果:
1
2
|
[8, 2, 6, 7, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
python---random模块详解的更多相关文章
- python time模块详解
python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明 一.简介 ...
- python docopt模块详解
python docopt模块详解 docopt 本质上是在 Python 中引入了一种针对命令行参数的形式语言,在代码的最开头使用 """ ""&q ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- python pathlib模块详解
python pathlib模块详解
- Python Fabric模块详解
Python Fabric模块详解 什么是Fabric? 简单介绍一下: Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...
- python time 模块详解
Python中time模块详解 发表于2011年5月5日 12:58 a.m. 位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python常用模块详解
python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...
- python os模块详解
一.Python os模块(Linux环境) 1.1 执行shell命令 os.system('cmd') 执行命令不保存结果 os.popen('command') 执行后返回结果,使用.read( ...
- Python ZipFile模块详解(转)
Python zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下,我们只需要使用这两个cl ...
随机推荐
- centos7搭建filebeat
filebeat的环境搭建 cd /home/elk wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.4 ...
- 学习CSS布局 - max-width
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux中断源码分析 - 概述(一)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 关于中断和异常 一般在书中都会把中断和异常一起说明,因为它们具有相同的特点,同时也有不同的地方.在CPU里,中断 ...
- 前后端交互json字符串
//将需要的参数转成json字符串,然后用utf-8编码 var obj = encodeURIComponent(JSON.stringify(this.categories),"utf- ...
- 【Linux系统目录结构】
登录系统后,在当前命令窗口下输入 ls / 你会看到 以下是对这些目录的解释: /bin bin是Binary的缩写.这个目录存放着最经常使用的命令. /boot 这里存放的是启动Linux时使用的一 ...
- Luogu3350 ZJOI2016 旅行者 最短路、分治
传送门 题意:给出一个$N \times M$的网格图,边有边权,$Q$组询问,每组询问$(x_1,y_1)$到$(x_2,y_2)$的最短路.$N \times M \leq 2 \times 10 ...
- EF 利用PagedList进行分页并结合查询 方法2
微软提供了PagedList分页,相信大家在网上也能搜索一大堆关于pagedList用法的博客,论坛.但是,在使用的过程中一不小心,就会掉入pagedList某种常规用法的陷阱. 我所说的某种常规用法 ...
- [Oracle]跨DBLINK的JOIN查询的数据库缓存问题15783452141
客户问到跨DBLINK,结合本地表和远端表的时候,数据在哪一边 的 Data Buffer 缓存. 测试的结果是:本地表在本地缓存,远端表在远端缓存. ####Testcase-0929-10 本地数 ...
- bitcoin 源码解析 - 交易 Transaction(三) - Script
bitcoin 源码解析 - 交易 Transaction(三) - Script 之前的章节已经比较粗略的解释了在Transaction体系当中的整体运作原理.接下来的章节会对这个体系进行分解,比较 ...
- JMeter:响应结果乱码解决方法
JMeter:响应结果乱码解决方法 我们经常使用jmeter做接口测试或者正则匹配 看到的响应结果存在乱码,这是小白经常会问的问题,这是因为jmeter会按照jmeter.properties文件中, ...