一 random 随机模块

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

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

执行

[root@node10 python]# python3 test.py
0.1808803715859979
[root@node10 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)

执行

[root@node10 python]# python3 test.py
1
9
7

1.3 randint()

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

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

执行

[root@node10 python]# python3 test.py
5
[root@node10 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)

执行

[root@node10 python]# python3 test.py
6.236326270460472
-1.5051738051095427
[root@node10 python]# python3 test.py
5.575905200548584
4.156048569051353
[root@node10 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)

执行

[root@node10 python]# python3 test.py
1
[root@node10 python]# python3 test.py
3
[root@node10 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())

执行

[root@node10 python]# python3 test.py
6
[root@node10 python]# python3 test.py
90
[root@node10 python]# python3 test.py
3

1.6 sample()

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

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

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

执行

[root@node10 python]# python3 test.py
['王文', '张学友']
[root@node10 python]# python3 test.py
['刘德华', '张学友']

1.7 shuffle()

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

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

执行

['王文', '王宝强', '宋小宝', '周杰伦', '张学友', '刘德华', '李宇春']
[root@node10 python]# python3 test.py
['张学友', '周杰伦', '王宝强', '王文', '宋小宝', '李宇春', '刘德华']
[root@node10 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)

执行

[root@node10 python]# python3 test.py
171ZV
[root@node10 python]# python3 test.py
UYj8P
[root@node10 python]# python3 test.py
JkNev
[root@node10 python]# python3 test.py
KCEY8
[root@node10 python]# python3 test.py
YMa30

二 Time时间模块

2.1 time()

获取本地时间戳

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

执行

[root@node10 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)

执行

[root@node10 python]# python3 test.py
1557692460.0

2.3 localtime()

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

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

执行

[root@node10 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)

执行

[root@node10 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)

执行

[root@node10 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))

执行

[root@node10 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)

执行

[root@node10 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)

执行

[root@node10 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)

执行

[root@node10 python]# python3 test.py
252269.756153608
44.59376426698873

023.Python的随机模块和时间模块的更多相关文章

  1. python常用模块之时间模块

    python常用模块之时间模块 python全栈开发时间模块 上次的博客link:http://futuretechx.com/python-collections/ 接着上次的继续学习: 时间模块 ...

  2. Python入门基础学习(时间模块,随机模块)

    Python基础学习笔记(六) time模块: 时间的三种表示方法: 1.格式化字符串 2.时间戳 用来表示和1970年的时间间隔,单位为s 3.元组 struct_time 9个元素 time的st ...

  3. python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块

    一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...

  4. random随机模块,time时间模块

    random /随机模块: 作用: 在某个范围内取到每一个值得概率是相通的. 一.随机小数 random.random() import random print(random.random())  ...

  5. python基础 ---time,datetime,collections)--时间模块&collections 模块

    python中的time和datetime模块是时间方面的模块 time模块中时间表现的格式主要有三种: 1.timestamp:时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算 ...

  6. Python学习总结14:时间模块datetime & time & calendar (一)

    Python中的常用于处理时间主要有3个模块datetime模块.time模块和calendar模块. 一.time模块 1. 在Python中表示时间的方式 1)时间戳(timestamp):通常来 ...

  7. python语言(四)关键字参数、内置函数、导入第三方模块、OS模块、时间模块

    一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会 ...

  8. python常用标准库(时间模块 time和datetime)

    常用的标准库 time时间模块 import time time -- 获取本地时间戳 时间戳又被称之为是Unix时间戳,原本是在Unix系统中的计时工具. 它的含义是从1970年1月1日(UTC/G ...

  9. python模块之时间模块

    一.time模块 表示时间的方式分为: 1时间戳(timestamp) 2格式化化的时间字符串(format string) 3结构化时间(struct_time) import time print ...

随机推荐

  1. yum 安装 Mysql error ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 开启远程连接 修改登入密码 忘记root密码 配置防火墙规则 随手mark

    yum 安装 MYsql:        yum install mysql mysql-server mysql-devel -y 1.1 登入报错: ERROR 1045 (28000): Acc ...

  2. jar类库加载顺序

    当我们启动一个tomcat的服务的时候,jar包和claess文件加载顺序: 1. $java_home/lib 目录下的java核心api 2. $java_home/lib/ext 目录下的jav ...

  3. java: 集合collection

    collection是集合层次结构中的根接口,一些集合允许重复元素,而其他集合不允许. 有些collection是有序的,而另一些是无序的. JDK不提供此接口的任何直接实现:它提供了更具体的子接口的 ...

  4. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  5. POJ 3258:River Hopscotch 二分的好想法

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9326   Accepted: 4016 D ...

  6. Java 跨系统开发隐患(一)

    换行符 主流系统换行符如下: Windows : \r\n Linux : \n Unix : \r 为了保证代码可以跨系统开发或使用,建议使用换行符时用下列语句获取: System.getPrope ...

  7. Linux误删所有内核,恢复内核的解决办法

    前言 我用df -h命令查看磁盘使用情况的时候发现,系统根目录空间已经比较小了,于是我就使用clean命令对系统内核进行清理,一不小心,就把所有的内核删除了,你很有可能也是我的这种经历,非常的崩溃.好 ...

  8. 洛谷 AT2827 LIS

    题目传送门 解题思路: 用f[i]表示长度为i的最长上升子序列的最小的末尾. AC代码: #include<iostream> #include<cstdio> #includ ...

  9. 手机与Arduino蓝牙串口通讯实验及完整例程

    安卓手机与Arduino之间采用蓝牙串口通讯,是很多智能装置和互动装置常用的控制方法,简单而有效,无需网络环境,很实用的技术. 实验采用Arduino UNO板,加了一块1602LCD屏做显示(因为只 ...

  10. 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL

    在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...