Python random模块(获取随机数)常用方法和使用例子
random.random
random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
random.uniform
random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a
print random.uniform(10, 20)
print random.uniform(20, 10)
# 18.7356606526
# 12.5798298022
random.randint
random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
print random.randint(12, 20) # 生成的随机数 n: 12 <= n <= 20
print random.randint(20, 20) # 结果永远是20
# print random.randint(20, 10) # 该语句是错误的。下限必须小于上限
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) 等效
random.choice
random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。下面是使用choice的一些例子:
print random.choice("学习Python")
print random.choice(["JGood", "is", "a", "handsome", "boy"])
print random.choice(("Tuple", "List", "Dict"))
random.shuffle
random.shuffle(x[, random]),用于将一个列表中的元素打乱。如:
p = ["Python", "is", "powerful", "simple", "and so on..."]
random.shuffle(p)
print p
# ['powerful', 'simple', 'is', 'Python', 'and so on...']
random.sample
random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5) # 从list中随机获取5个元素,作为一个片断返回
print slice
print list # 原有序列并没有改变
随机整数:
>>> random.randint(0,99)
# 21
随机选取0到100间的偶数:
>>> random.randrange(0, 101, 2)
# 42
随机浮点数:
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
# 5.4221167969800881
随机字符:
>>> random.choice('abcdefg&#%^*f')
# 'd'
多个字符中选取特定数量的字符:
random.sample('abcdefghij', 3)
# ['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import string
>>> string.join( random.sample(['a','b','c','d','e','f','g','h','i','j'], 3) ).replace(" ","")
# 'fih'
随机选取字符串:
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
# 'lemon'
洗牌:
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
# [3, 2, 5, 6, 4, 1]
相关文章
- python 模块 chardet下载方法及介绍
- Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)
- python语法基础_01.Python基础
- python执行脚本加参数_命令行运行Python脚本时传入参数的三种方式详解以及argparse子命令subparsers()方法
- python魔法方法之-Python __repr__()方法:显示属性
- python 保存图片_用Python从视频中提取每一帧的图片
- Python学习-将list列表写入文件并读取方法汇总
- Python的re模块的match和fullmatch方法的区别
- python批量提取word文档中的图片(含图片格式转换和GUI)
- python课程:python3的数字与字符串
- Python 使用ntplib库同步校准当地时间的方法 (NTP)
- 中途转行python?怎么学?没有基础的我30了自学Python转行靠谱吗?
- python中的__call__()方法
- python中的print函数重复输出30个字符及字符串的方法
- 手把手|用Python端对端数据分析识别机器人“僵尸粉”
- Linux执行Python脚本
- 找出只出现一次的数字-Python
- python str方法之ljust、rjust、center
- 《Python面向对象编程指南》——2.2 __format__()方法
- 《Python面向对象编程指南》——2.3 __hash__()方法
- python学习之基于Python的人脸识别技术学习
- Python IDE 安装 PyCharm 的正确姿势以及更新方法
- Python 教程之如何使用 matplotlib 在 python 中绘制数学函数
- 迎新年倒计时python代码(带背景及背景音乐)
- Python中函数、方法的区别
- Python类的定义、方法和属性使用
- python-关于类、方法、对象(实例):通过一个例子看一下self都做了哪些事情
- 【Python】python 库分类大全
- Selenium4 Python实现Page Factory设计模式,python新的定位方法