Python Random模块
2015-07-13 09:01
671 查看
构造随机是程序中常用的功能,Python内置了这方面的支持,简洁又高效。这篇博客主要记录一下Random中常用的几个函数功能。
Return the next random floating point number in the range [0.0, 1.0).
Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
实现方式:a + (b-a) * random()
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
random.random():返回一个零到一之间左闭右开的浮点数。
Return the next random floating point number in the range [0.0, 1.0).
random.uniform(a, b):返回a到b之间的一个浮点数。
Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
实现方式:a + (b-a) * random()
random.randint(a, b):返回a到b之间的整数,包括a和b。
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
random.choice(seq): 返回序列seq中的一个随机元素,如果序列为空,则会报错。
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.
random.randrange(stop) ,random.randrange(start, stop[, step]): 等同于choice(range(start, stop, step))。但不会创建range对象。
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
random.sample(population, k): 返回一个列表,为population中前K个元素的乱序。
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
random.shuffle(x[, random]): 本身没有返回值,作用为将x中的元素打乱。
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
相关文章推荐
- Python笔记:Python版本升级
- monkeyrunner截图限时免费阅读#1
- python简单获取数组元素个数的方法
- 用python搭建一个web service 接口 ( RESTful API )
- 【python】13位随机序列号生成工具 源码分析
- python用for循环实现冒泡排序
- Python中的正则表达式
- 如何写出优雅的Python之设置class缺省值
- python读写文件
- Python项目六: 使用CGI进行远程编辑
- Python图像处理(16):图像金字塔
- Python格式化字符串
- python初学遇到的问题总结
- 使用python的numpy时 导致的问题
- win7下python执行cmd命令中exe崩溃的处理
- Java,php,python,js数据类型
- Python+Django+SAE系列教程10-----Django模板
- Python学习记录
- python 循环中break与continue区别学习
- Python 获得Facebook用户有一个共同的兴趣Friends