python,random随机数的获取
随机数生成
首先我们需要在程序中引入random》》》import random as r
r.random()用于生成一个随机的浮点数,
>>> print(r.random()) 0.23928059596578843
>>>
r.uniform(10,20),生成一个随机的浮点数,如果a>b 则a为上限,b为下限。如果a<b,则b为上限
>>> print(r.uniform(10,20)) 15.995495884011348
>>> print(r.uniform(20,10)) 13.179092381602349
>>>
#如果没有给定a,b那么会报错
>>> print(r.uniform())
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(r.uniform())
TypeError: uniform() missing 2 required positional arguments: 'a' and 'b'
>>> print(r.randint())
r.randint(a,b),生成一个随机的整数,a 是下限,b是上限。下限必须小于上限
>>> print(r.randint()) Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print(r.randint())
TypeError: randint() missing 2 required positional arguments: 'a' and 'b'
>>> print(r.randint(1,10)) 1
>>> print(r.randint(10,1))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
print(r.randint(10,1))
File "D:\python\lib\random.py", line 222, in randint
return self.randrange(a, b+1)
File "D:\python\lib\random.py", line 200, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (10,2, -8)
>>>
r.randrange(a,b,1or2),随机取出指定范围内的奇偶数,1奇数 2偶数
>>> r.randrange(0,10,2) 6
>>> r.randrange(0,10,1) 1
>>> r.randrange(0,10,1) 7
>>>
r.choice('qwertuiopolkjhgffdsa')所及取出某一个字符
>>> print(r.choice('qwertyuiop[]lkjhgfdsazxcvbnm'))
k
>>> print(r.choice('qwertyuiop[]lkjhgfdsazxcvbnm'))
n
>>> print(r.choice('qwertyuiop[]lkjhgfdsazxcvbnm'))
t
>>>
r.sample(str,num),会随机输出num个字符,且num一定小于等于len(str)
# 小于字符长度时
>>> r.sample('qweasd',2) ['d', 'q']
# 大于字符长度时
>>> r.sample('qweasd',8) Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
r.sample('qweasd',8)
File "D:\python\lib\random.py", line 319, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
# 等于字符长度时
>>> r.sample('qweasd',6) ['s', 'd', 'a', 'e', 'w', 'q']
>>>
洗牌,也就是随机排序
>>> items = [1,2,3,4,5,6] >>> r.shuffle(items) >>> items [3, 6, 1, 5, 2, 4]
>>>
python,random随机数的获取的更多相关文章
- Python random模块(获取随机数)
1.random.random 随机生成一个0到1的随机浮点数: 0 <= n < 1.0 In [2]: print random.random() 0.544824016934 2.r ...
- Python Random随机数
Python产生随机数的功能在random模块中实现.实现了各种分布的伪随机数生成器 该模块能够生成0到1的浮点随机数,也能够在一个序列中进行随机选择.产生的随机数能够是均匀分布.高斯分布,对数正态分 ...
- Python random模块(获取随机数)常用方法和使用例子
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- python random模块(获取随机数)的常用方法及示例
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- Python random模块 例子
最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random random.random()用于生成一个0到1的随机符点数: ...
- python random 的用法
python random的里面的方法其实是Random实例化的对象. 里面几个常用的几个方import random print( random.randint(1,10) ) # 产生 1 到 1 ...
- js random获取随机数,获取任意范围内随机整数
壹 ❀ 引 想着好久没做笔试题了,去GitHub找了面试相关的项目,结果被第一道题难住了.....说难其实也不难,而是我忘记了取范围随机整数怎么写了,不可否认如果当时是我在笔试,肯定也凉了,那么就由 ...
- python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET
python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET python random模块 分类: python 2011-11-15 15:31 6037人阅读 评论(2) ...
- (转)Python random模块
原文:https://my.oschina.net/cuffica/blog/33336 https://www.cnblogs.com/renpingsheng/p/7105296.html ran ...
随机推荐
- spring mvc后端校验validator
springmvc中我们使用hibernate的校验框架validation: pom: <validator.version>5.2.4.Final</validator.vers ...
- 一个关于finally和return的面试题
public class Test{ public int add(int a,int b){ try { return a+b; } catch (Exception e) { System.out ...
- 配置taBar所遇见的问题(踩坑之路)
目前效果图: 问题:我遇见一个问题,点击每周关注的时候,他应该跳转到哪一个页面.在没有设置taBar还是可以跳转的. 解决方法是: 修改 open-type='navigate'为switchTab( ...
- pxc5.7配置安装
pxc5.7配置安装 一.准备工作 # Centos6. 最小化安装操作系统 ########################---- Centos最小化安装推荐常用依赖包---- ######### ...
- maven直接饮用jar包的写法
<dependency> <groupId>sample</groupId> <artifactId>com.sample</artifactId ...
- powershell 定时删除脚本
powershell 定时删除脚本 $today=Get-Date #"今天是:$today" #昨天 #"昨天是:$($today.AddDays(-1))" ...
- myeclipse使用步骤总结
1.安装和破解:http://www.cnblogs.com/haimishasha/p/5203069.html 2.修改编码方式:http://www.cnblogs.com/haimishash ...
- SQL Server2012安装流程
今天手比较抽风,把原来的SQL Server给卸载了,卸载还卸了半天,真是…… 安装时找了好多教程,结果都不是很详细,然后准备自己摸索一下,把这个过程记录下来,供大家参考,如果有不当的地方,欢迎指正, ...
- PHP7 学习笔记(十二)gRPC
GitHub:https://github.com/grpc/grpc/tree/master/src/php 环境:Linux + php7 1.安装grpc pecl install grpc 编 ...
- ArcGis Python脚本——批量对影像、要素类定义投影
这一段是批量定义要素类(FeatureClasses)投影的ArcPy代码: 把要处理的要素类塞进一个文件夹(工作空间,workspace),然后将代码开头的路径换成这个“文件夹”的路径,处理完后再做 ...