023.Python的随机模块

一 random 随机模块

1.1 获取随机0-1之间的小数(左闭右开)  0<= x < 1

import random
res = random.random()
print(res)

执行

[[email protected] python]# python3 test.py
0.1808803715859979
[[email protected] python]# python3 test.py
0.39177193259061716

1.2 randrange()

随机获取指定范围内的整数(包含开始值,不包含结束值,间隔值)

import random
res = random.randrange(3)  # 0~2
print(res)
res = random.randrange(1,10) # 1~9
print(res)
res = random.randrange(1,10,3) # 1 4 7
print(res)

执行

[[email protected] python]# python3 test.py
1
9
7

1.3 randint()

随机产生指定范围内的随机整数 (目前唯一可以取到最大值的函数,不推荐使用)

import randomres = random.randint(2,5) # 2 3 4 5 最大值可以取到
print(res)

执行

[[email protected] python]# python3 test.py
5
[[email protected] python]# python3 test.py
4

randint 必须给2个参数 1个或者3个都不行,功能有局限性,不推荐使用

import random
res = random.randint(2,5,2)
print(res)

执行

1.4 uniform()

获取指定范围内的随机小数(左闭右开)

import random
res = random.uniform(1,10) # 1<= x < 10
print(res)
res = random.uniform(5,-3)
print(res)

执行

[[email protected] python]# python3 test.py
6.236326270460472
-1.5051738051095427
[[email protected] python]# python3 test.py
5.575905200548584
4.156048569051353
[[email protected] python]# python3 test.py
1.9549944091757219
4.605001284532852

1.5 choice()

随机获取序列中的值(多选一)

import random
listvar = [1,2,3,90,6,5]
res = random.choice(listvar)
print(res)

执行

[[email protected] python]# python3 test.py
1
[[email protected] python]# python3 test.py
3
[[email protected] python]# python3 test.py
90
[roo

自定义函数 实现choice的效果

import random
listvar = [1,2,3,90,6,5]
def mychoice():
        length = len(listvar)
        res = random.randrange(0,length) # 0 ~ 5
        return  listvar[res]
print(mychoice())

执行

[[email protected] python]# python3 test.py
6
[[email protected] python]# python3 test.py
90
[[email protected] python]# python3 test.py
3

1.6 sample()

随机获取序列中的值(多选多) [返回列表]

sample(容器类型数据,选几个)

import random
listvar = ["周杰伦","李宇春","王宝强","宋小宝","刘德华","张学友","王文"]
res = random.sample(listvar,2)
print(res)

执行

[[email protected] python]# python3 test.py
[‘王文‘, ‘张学友‘]
[[email protected] python]# python3 test.py
[‘刘德华‘, ‘张学友‘]

1.7 shuffle()

随机打乱序列中的值(直接打乱原序列)

import randomlistvar = ["周杰伦","李宇春","王宝强","宋小宝","刘德华","张学友","王文"]
random.shuffle(listvar)
print(listvar)

执行

[‘王文‘, ‘王宝强‘, ‘宋小宝‘, ‘周杰伦‘, ‘张学友‘, ‘刘德华‘, ‘李宇春‘]
[[email protected] python]# python3 test.py
[‘张学友‘, ‘周杰伦‘, ‘王宝强‘, ‘王文‘, ‘宋小宝‘, ‘李宇春‘, ‘刘德华‘]
[[email protected] python]# python3 test.py
[‘李宇春‘, ‘刘德华‘, ‘王宝强‘, ‘王文‘, ‘周杰伦‘, ‘张学友‘, ‘宋小宝‘]

1.8 生成验证码

实现一个验证码功能,每次随机产生5个字符

import random
def yanzhengma():
        strvar = ‘‘
        for i in range(5):
                # res = chr(97)
                # a-z 范围的小写字母
                a_z = chr(random.randrange(97,123))
                # A-Z 产生所有的大写字母 65 => A  90
                A_Z = chr(random.randrange(65,91))
                # 0-9 产生0-9 10个数字
                num  = str(random.randrange(0,10)) # 为了实现字符串的拼接
                # 把范围的可能出现的字符放到同一的列表中进行随机挑选
                listvar = [a_z,A_Z,num]
                # 把选好的5个随机字符 通过+来形成拼接
                strvar += random.choice(listvar)
        # 直接返回该字符串
        return strvar
res = yanzhengma()
print(res)

执行

[[email protected] python]# python3 test.py
171ZV
[[email protected] python]# python3 test.py
UYj8P
[[email protected] python]# python3 test.py
JkNev
[[email protected] python]# python3 test.py
KCEY8
[[email protected] python]# python3 test.py
YMa30

二 Time时间模块

2.1 time()

获取本地时间戳

import time
res = time.time()
print(res)

执行

[[email protected] python]# python3 test.py
1574685161.550896

2.2 mktime()

通过[时间元组]获取[时间戳] (参数是时间元组)

import time
ttl = (2019,5,12,15,21,0,0,0,0)
res = time.mktime(ttl)
print(res)

执行

[[email protected] python]# python3 test.py
1557692460.0

2.3 localtime()

通过[时间戳]获取[时间元组] (默认当前时间)

import time
ttl = time.localtime()  # 默认使用当前时间戳
print(ttl)
ttl = time.localtime(1557645000) # 自定义时间戳,转化为时间元组
print(ttl)

执行

[[email protected] python]# python3 test.py
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=25, tm_hour=7, tm_min=36, tm_sec=43, tm_wday=0, tm_yday=329, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=12, tm_hour=3, tm_min=10, tm_sec=0, tm_wday=6, tm_yday=132, tm_isdst=1)

2.4 ctime()

通过[时间戳]获取[时间字符串] (默认当前时间)

import time
res = time.ctime()  # 默认使用当前时间戳
print(res)
res = time.ctime(1557645000) # 可以手动自定义时间戳
print(res)

执行

[[email protected] python]# python3 test.py
Mon Nov 25 07:38:28 2019
Sun May 12 03:10:00 2019

2.5 asctime()

通过[时间元组]获取[时间字符串](参数是时间元组)

import time
ttl = (2019,5,12,15,21,0,1,0,0)
res = time.asctime(ttl)
print(res)

执行

[[email protected] python]# python3 test.py
Tue May 12 15:21:00 2019

优化版

import time
ttl = (2019,5,12,15,21,0,4,0,0)
res = time.mktime(ttl)
print(time.ctime(res))

执行

[[email protected] python]# python3 test.py
Sun May 12 16:21:00 2019

2.6 strftime()

通过[时间元组]格式化[时间字符串]  (格式化字符串,[可选时间元组参数])

import time
res = time.strftime("%Y-%m-%d %H:%M:%S") # 默认以当前时间戳转化为字符串
print(res)
# linux当中 strftime可以识别中文,windows不行
res = time.strftime("%Y-%m-%d %H:%M:%S",(2008,8,8,8,8,8,0,0,0))
print(res)

执行

[[email protected] python]# python3 test.py
2019-11-25 07:42:20
2008-08-08 08:08:08

2.7 strptime()

通过[时间字符串]提取出[时间元组]  (时间字符串,格式化字符串)

注意:左右两侧的字符串要严丝合缝,有多余的空格都不行,然后按照次序,依次通过格式化占位符,提取时间

import time
res = time.strptime("2019年3月8号15点21分30秒,发射了人造卫星嫦娥"  ,  "%Y年%m月%d号%H点%M分%S秒,发射了人造卫星嫦娥")
print(res)

执行

[[email protected] python]# python3 test.py
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=8, tm_hour=15, tm_min=21, tm_sec=30, tm_wday=4, tm_yday=67, tm_isdst=-1)

2.8 perf_counter()

用于计算程序运行的时间

import time
startime = time.perf_counter()
print(startime)
for i in range(1000000000):
        pass
endtime = time.perf_counter()
# 结束时间  - 开始时间[ time.time()] 也可以实现;
res = endtime - startime
print(res)

执行

[[email protected] python]# python3 test.py
252269.756153608
44.59376426698873

原文地址:https://www.cnblogs.com/zyxnhr/p/12288723.html

时间: 2024-08-03 03:24:06

023.Python的随机模块的相关文章

【Python】随机模块random &amp; 日期时间のtime&amp;&amp;datetime

■ random 顾名思义,random提供了python中关于模拟随机的一些方法.这些方法都一看就懂的,不多说了: random.random() 返回0<n<=1的随机实数 random.uniform(a,b) 返回a<n<=b的随机实数 random.randrange([start],stop,[step]) 返回序列range(start,stop,step)中随机一项 random.choice(seq) 返回序列中随机一项 random.sample(seq,n)

python的内置模块random随机模块方法详解以及使用案例(五位数随机验证码的实现)

1.random(self): Get the next random number in the range [0.0, 1.0) 取0到1直接的随机浮点数 import random print(random.random()) C:\python35\python3.exe D:/pyproject/day21模块/random随机模块.py 0.3105503800442595 2.randint(self, a, b) Return random integer in range [a

Python:随机生成测试数据的模块--faker的基本使用

本文内容: faker的介绍 faker的使用 小例子:生成随机的数据表信息 首发日期:2018-06-15 faker介绍: faker是python的一个第三方模块,是一个github上的开源项目. 主要用来创建一些测试用的随机数据. 官方文档:https://faker.readthedocs.io/en/master/index.html faker的使用: 1.安装模块 pip3 install Faker [使用faker也能识别成功,不过新版已经更新为Faker] 2.导入模块 f

python中的关键字---5(时间/随机模块)

模块 什么是模块?模块就是别人写好的代码,放在一个py文件里,给你使用 模块有几种?三种 : 内置模块\第三方模块\自定义模块 模块是好的 为什么要有模块? 写好了之后直接给你用 - 方便了用户的使用,提高了开发效率 为了节省内存,所以把常用的(但是不是每一个程序都必须用到的)函数\方法 根据分类来将这些函数分到不同的文件中存储起来 所以我们用到哪一类方法,才把这类方法所在的模块导入到内存中,能够最大限度的节省内存 内置函数 是每一个py程序在启动的时候都会加载到内存空间中的内置模块 是以文件的

Python常用模块——random随机模块

Python常用模块--random随机模块 程序中有很多地方都需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串. >>> random.randrange(1,10) #返回1-10之间的一个随机数,不包括10 >>> random.randint(1,10) #返回1-10之间的一个随机数,包括10 >>> random.randrange(0, 100, 2) #随机选取0到100间的偶数,即步长为2 &g

Python全栈开发——时间模块和随机模块

#时间模块 import time import datetime print(datetime.datetime.now()) #时间戳 print(time.time()) #将时间戳转化为结构化时间 print(time.localtime(time.time())) #将结构化时间转化为时间戳 print(time.mktime(time.localtime())) #将结构化时间转化为字符串时间 strftime print(time.strftime("%Y---%m-%d %X&q

Python基础-----random随机模块(验证码)

random随机模块的用法及功能 import random print(random.random())#(0,1)----获取0-1中的一个float print(random.randint(1,3)) #[1,3]取范围内的一个整数 print(random.randrange(1,3)) #[1,3)取范围内的一个整数 print(random.choice([1,'23',[4,5]]))#23 随机获取可迭代对象中的一个元素 print(random.sample([1,'23',

python之OS模块(对文件or目录操作)

OS模块 os,语义为操作系统,包含普遍的操作系统功能,与具体的平台无关.python编程时,处理文件和目录这些操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小-- os模块不受平台限制,也就是说:当我们要在linux中显示当前命令时就要用到pwd命令,而Windows中cmd命令行下就要用到这个,例如:这时候我们使用python中os模块的os.path.abspath(name)功能,甭管是linux或者Windows都可以获取当前的绝对路径. 常见函数列表 os.name

PYTHON学习第二模块 python内置模块介绍

1 >>> import time 2 >>> time.time() 3 1491064723.808669 4 >>> # time.time()返回当前时间的时间戳timestamp(定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数)的方法,无参数 5 >>> time.asctime() 6 'Sun Apr 2 00:39:32 2017' 7 >>> # time.asctim