python之random随机函数
random.random()用于生成
用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成随机数
|
1
|
n: a <= n <= b。如果 a <b, 则 b <= n <= a。 |
|
1
2
3
4
5
6
|
print random.uniform(10, 20) print random.uniform(20, 10) #---- #18.7356606526 #12.5798298022 random.randint |
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,Python生成随机数
|
1
2
3
|
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20 print random.randint(20, 20) #结果永远是20 #print random.randint(20, 10) #该语句是错误的。 |
下限必须小于上限。
random.randrange
从指定范围内,按指定基数递增的集合中 ,这篇文章就是对python生成随机数的应用程序的部分介绍。
随机整数:
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
python之random随机函数的更多相关文章
- python中的随机函数random的用法示例
python中的随机函数random的用法示例 一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据 ...
- python使用random函数生成随机数
python使用random函数来生成随机数,常用的方法有: import random #生成0-1之间的数(不包括0和1) random.random() #生成1-5之间的随机整数(包括1和5) ...
- 【转载】python 模块 - random生成随机数模块
随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...
- 【转】python之random模块分析(一)
[转]python之random模块分析(一) random是python产生伪随机数的模块,随机种子默认为系统时钟.下面分析模块中的方法: 1.random.randint(start,stop): ...
- python的random()函数
python 的random函数需要调用 #!/usr/bin/python # -*- coding: UTF-8 -*- import random print( random.randint(1 ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- python随机数random
1.random.random()方法用于生成一个0到1的随机浮点数:0<=n<1.0 2.random.uniform(a,b):用于生成一个指定范围内的随机浮点数,两格参数中,其中一个 ...
- ZH奶酪:【Python】random模块
Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random ra ...
- python基础--random模块
python使用random生成随机数 下面是主要函数random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0random.randint(a, b)生成的 ...
随机推荐
- luoguP4578_ [FJOI2018]所罗门王的宝藏
题意 一个n*m的矩阵,初始值全为0,每一行每一列操作一次可以加1或者减1,问能否操作得到给定矩阵. 分析 行和列的分别的加减是可以相互抵消的,因此我们只需要考虑行的加和列的减. 对于给定矩阵每一个数 ...
- 帝国cms 项目搬家换域名修改详情页图片路径
update phome_ecms_news_data_1 set newstext=REPLACE (newstext,'/d/file/','http://www.xxxx.com/d/file/ ...
- SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...
- 聚类算法博客 K-means算法
最近看到一个 blog 感觉超好.记录下.. http://blog.pluskid.org/?p=17
- centos7搭建安装superset
superset官网: https://superset.incubator.apache.org/ 系统环境:system:centos7 一.安装工具及依赖包安装工具包:yum -y instal ...
- scrapy 运行时报错 No module named _sqlite3
新服务器上运行scrapy时报错 exceptions.ImportError: No module named _sqlite3 原因 由于新的环境缺少sqlite的依赖,编译python3是虽然不 ...
- 【转】在Linux下写一个简单的驱动程序
转自:https://www.cnblogs.com/amanlikethis/p/4914510.html 本文首先描述了一个可以实际测试运行的驱动实例,然后由此去讨论Linux下驱动模板的要素,以 ...
- php set_magic_quotes_runtime() 函数过时解决方法
PHP5.3中 bool set_magic_quotes_runtime ( bool $new_setting )函数过时.把函数: set_magic_quotes_runtime($newse ...
- for 循环,range()函数和导入模块
(一)for语句构成: for i in range(5): print('My name is') for i in range(5): print('Jimmy Five Times ('+str ...
- rsync服务端排错思路
rsync服务端排错思路 rsync服务端排错思路 查看rsync服务配置文件路径是否正确,正确的默认路径为/etc/rsyncd.conf 查看配置文件里host allow,host ...