模块之-random(随机模块)

random   #shuffle 洗牌功能

>>> i=[1,2,3,4,5,6]
>>> random.shuffle(i)
>>> random.shuffle(i)
>>> i
[2, 5, 6, 1, 3, 4] #uniform 就是在random.random()的基础上指定个区间的浮点数
>>> random.uniform(1,4)
3.3291495463557723
>>> random.uniform(1,4)
2.3558103852278887 >>> import random
>>> print (random.random()) #随机的(0-1)的一个浮点数。
0.039995559750025445
>>> print (random.random())
0.7594093067400499 >>> random.sample('hello',2) #取两个字符
['e', 'l'] >>> random.choice([1,4,5]) #随机取后面的列表的数字
5
>>> random.choice([1,4,5])
1
>>> random.choice([1,4,5])
1 >>> random.randrange(1,3)#两头都要有效。
1
>>>
>>> random.randrange(1,3)
2 >>> for i in range(3): #顾头不顾尾(0,1,2)
... print(i)
...
0
1
2 >>> random.randint(1,3) # 指定后面头尾都要算的随机整数
2
>>> random.randint(1,3)
3 生成一个4位的字母和数字的随机码 import random
checkcode=''
for i in range(4):
current=random.randrange(0,4)
if current==i:
tmp=chr(random.randint(65,90))
else:
tmp=random.randint(0,9) checkcode+=str(tmp)
print(checkcode) 打印结果 J384

模块之-random(随机模块)的更多相关文章

  1. python常用标准库(math数学模块和random随机模块)

    常用的标准库 数学模块 import math ceil -- 上取整 对一个数向上取整(进一法),取相邻最近的两个整数的最大值. import math res = math.ceil(4.1) p ...

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

    1.random(self): Get the next random number in the range [0.0, 1.0) 取0到1直接的随机浮点数 import random print( ...

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

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

  4. python模块知识二 random -- 随机模块、序列化 、os模块、sys -- 系统模块

    4.random -- 随机模块 a-z:97 ~ 122 A-Z :65 ~ 90 import random #浮点数 print(random.random())#0~1,不可指定 print( ...

  5. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  6. Python—时间模块(time)和随机模块(random)

    时间模块 time模块 获取秒级时间戳.毫秒级时间戳.微秒级时间戳 import time t = time.time() print t # 原始时间数据 1574502460.90 print i ...

  7. random(随机模块)

    程序中有很多地方需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串 >>> random.randrange(1,10) #返回1-10之间的一 ...

  8. day22 Pythonpython random随机模块:略!!!本文os模块

    OS模块 用于提供系统级别的操作: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...

  9. Python---基础---常用的内置模块(Github、P有charm、math数学模块和random随机数模块,做一些简单的练习)

    2019-05-24 ----------------------------------

随机推荐

  1. oracle增、删、改、查

    参照文档 https://blog.csdn.net/yes_is_ok/article/details/79271965 https://blog.csdn.net/cl723401/article ...

  2. I'm using Python 2.7.2 ImportError No module named bz2

    安装mongodb的时候 提示错误 这是因为同时装了python2.6和2.7的缘故 sudo cp /usr/lib64/python2.6/lib-dynload/bz2.so /usr/loca ...

  3. react中递归生成列表

    import React, {Component} from 'react'; import { Menu, Icon } from 'antd'; import {Link} from 'react ...

  4. 使用torchvision下载数据集显示没有模板

    配置: Anaconda+Windows+py3.7 一:首先先卸载开始的torchvision:pip uninstall torchvision 二:找到官网:https://pypi.org/p ...

  5. 5分钟了解图数据库Neo4j的使用

    1.图数据库安装与配置 1.1安装与配置 配置path = %NEO4J_HOME%\bin   启动命令:neo4j console   web访问:http://localhost:7474 1. ...

  6. ubuntu 16.04 server 扩容(LVM)磁盘

    因为发现我的本地server出现磁盘满了的情况 所以进行lvm的扩容 1 查看磁盘情况 df -h 原本发现 /dev/mapper/ubuntu1604--vg-root 这个磁盘满了 所以要进行扩 ...

  7. Spring Boot(2)中的yaml配置简介

    搞Spring Boot的小伙伴都知道,Spring Boot中的配置文件有两种格式,properties或者yaml,一般情况下,两者可以随意使用,选择自己顺手的就行了,那么这两者完全一样吗?肯定不 ...

  8. 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引

    数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA

  9. windows 装mac

    必备条件: 1.vmware虚拟机 2.给相应版本虚拟机打mac补丁 3.用securable检测CPU支持虚拟化设置 4.mac镜像文件 5.这时候还不能启动虚拟机,还需要在引导文件里面进行参数修改 ...

  10. windows10 AppStore安装 应用商店重新安装

    点击左下角的搜索按钮,如下图所示   输入powershell,在结果中找到widows powershell应用,如下图所示   右键单击widows powershell应用,选择以管理员运行,如 ...