Python常用随机数与随机字符串方法实例


Posted in Python onApril 09, 2015

随机整数:

>>> 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]

random的函数还有很多,此处不一一列举,
参考资料: http://docs.python.org/lib/module-random.html
Python 相关文章推荐
Python中的生成器和yield详细介绍
Jan 09 Python
Cpy和Python的效率对比
Mar 20 Python
一步步解析Python斗牛游戏的概率
Feb 12 Python
Python 实现随机数详解及实例代码
Apr 15 Python
Python程序退出方式小结
Dec 09 Python
pyqt5 实现多窗口跳转的方法
Jun 19 Python
使用python实现男神女神颜值打分系统(推荐)
Oct 31 Python
Pytorch Tensor 输出为txt和mat格式方式
Jan 03 Python
pycharm中导入模块错误时提示Try to run this command from the system terminal
Mar 26 Python
python如何爬取网页中的文字
Jul 28 Python
C++和python实现阿姆斯特朗数字查找实例代码
Dec 07 Python
用Python实现一个打字速度测试工具来测试你的手速
May 28 Python
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
Apr 09 #Python
举例讲解Python程序与系统shell交互的方式
Apr 09 #Python
使用Python中的cookielib模拟登录网站
Apr 09 #Python
列举Python中吸引人的一些特性
Apr 09 #Python
Python的Bottle框架的一些使用技巧介绍
Apr 08 #Python
在Python的框架中为MySQL实现restful接口的教程
Apr 08 #Python
简单介绍Python的轻便web框架Bottle
Apr 08 #Python
You might like
《斗罗大陆》六翼天使武魂最强,为什么老千家不是上三宗?
2020/03/02 国漫
用header 发送cookie的php代码
2007/03/16 PHP
php 5.3.5安装memcache注意事项小结
2011/04/12 PHP
PHP使用feof()函数读文件的方法
2014/11/07 PHP
PHP中in_array函数使用的问题与解决办法
2016/09/11 PHP
PHP实现的最大正向匹配算法示例
2017/12/19 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
javascript模仿msgbox提示效果代码
2008/06/10 Javascript
jQuery插件jQuery-JSONP开发ajax调用使用注意事项
2013/11/22 Javascript
window.location.href IE下跳转失效的解决方法
2014/03/27 Javascript
JavaScript中对象属性的添加和删除示例
2014/05/12 Javascript
Node.js中JavaScript操作MySQL的常用方法整理
2016/03/01 Javascript
整理关于Bootstrap排版的慕课笔记
2017/03/29 Javascript
Vue键盘事件用法总结
2017/04/18 Javascript
微信小程序 监听手势滑动切换页面实例详解
2017/06/15 Javascript
浅谈angular4实际项目搭建总结
2017/12/01 Javascript
Vue.js图片预览插件使用详解
2018/08/27 Javascript
js canvas实现橡皮擦效果
2018/12/20 Javascript
完美解决vue 中多个echarts图表自适应的问题
2020/07/19 Javascript
微信小程序入门之绘制时钟
2020/10/22 Javascript
快速排序的算法思想及Python版快速排序的实现示例
2016/07/02 Python
Python用 KNN 进行验证码识别的实现方法
2018/02/06 Python
python代码过长的换行方法
2018/07/19 Python
ubuntu上安装python的实例方法
2019/09/30 Python
Python模拟登录requests.Session应用详解
2020/11/17 Python
HTML5新增的Css选择器、伪类介绍
2013/08/07 HTML / CSS
一套英文Java笔试题面试题
2016/04/21 面试题
小学教师的自我评价范例
2013/10/31 职场文书
学校周年庆活动方案
2014/08/22 职场文书
公司租车协议书
2015/01/29 职场文书
入党介绍人意见怎么写
2015/06/03 职场文书
天堂的孩子观后感
2015/06/11 职场文书
当你找不到方向的时候,不妨读读刘备的一生
2019/08/05 职场文书
实体类或对象序列化时,忽略为空属性的操作
2021/06/30 Java/Android
微信小程序APP的生命周期及页面的生命周期
2022/04/19 Javascript
JS前端使用canvas实现扩展物体类和事件派发
2022/08/05 Javascript