import random

# 方法返回随机生成的一个实数,它在[0,1)范围内
print(random.random())
运行结果:
0.06435148447021877

# 方法返回随机生成的一个整数,这里包括 8
print(random.randint(1, 8))
运行结果:
1

# 返回一个列表,元组或字符串的随机项
print(random.choice('hello'))
运行结果:
l

print(random.choice(['hello', 11, [22]]))
运行结果:
[22]

# 从 list 中随机获取 2 个元素,作为一个列表返回 
print(random.sample(['123', 4, [1, 2]], 2))
运行结果:
['123', [1, 2]]

# 随机获取5个验证码
def v_code():
    code = ''
    for i in range(5):
       code += str(random.choice([random.randint(0, 9), chr(random.randint(65, 91))]))

print(code)

v_code()
运行结果:
33NSI

random module的更多相关文章

  1. Python从题目中学习:random() module

    最近在给公司培训Python,布置了一道题: ----------------------------------------------------------------------------- ...

  2. python的random模块

    As an example of subclassing, the random module provides the WichmannHill class that implements an a ...

  3. Python3模块-random、hashlib和base64

    random模块 random.random()用于生成一个浮点数x,范围为0 =< x < 1 import random >>>print(random.random ...

  4. Random numbers

    Most computer programs do the same thing every time they execute, given the same inputs, so they are ...

  5. python 中的metaclass和baseclasses

    提前说明: class object  指VM中的class 对象,因为python一切对象,class在VM也是一个对象,需要区分class对象和 class实例对象. class instance ...

  6. Brief Tour of the Standard Library

    10.1. Operating System Interface The os module provides dozens of functions for interacting with the ...

  7. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习

    # Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...

  8. 用JSON-server模拟REST API(二) 动态数据

    用JSON-server模拟REST API(二) 动态数据 上一篇演示了如何安装并运行 json server , 在这里将使用第三方库让模拟的数据更加丰满和实用. 目录: 使用动态数据 为什么选择 ...

  9. 转:Python获取随机数(英文)

    Random - Generate pseudo-random numbers Source code: Lib/random.py This module implements pseudo-ran ...

随机推荐

  1. pyhton-函数初级

    f = open("司马光砸缸", mode="r+", encoding="utf-8") f.seek(12) f.truncate() ...

  2. [POJ2761]Feed the dogs

    Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...

  3. EF-记录程序自动生成并执行的sql语句日志

    在EntityFramework的CodeFirst模式中,我们想将程序自动生成的sql语句和执行过程记录到日志中,方便以后查看和分析. 在EF的6.x版本中,在DbContext中有一个Databa ...

  4. Cracking The Coding Interview2.4

    删除前面的linklist,使用node来表示链表 // You have two numbers represented by a linked list, where each node cont ...

  5. 无法获取 vmci 驱动程序版本: 句柄无效

    https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 将 vmci0.present = "TURE" 改 ...

  6. mybatis学习(五)----实现关联表查询

    一.一对一的表查询 查询班级表中班级号为1的对应的记录(包括教师的具体信息) 1.首先建立数据表 数据表class和techear,class表中只有一个外键techear_id,sql脚本如下: C ...

  7. cxf http 代码自动生成

    1.下载 cxf 直接进入镜像下载http://mirrors.tuna.tsinghua.edu.cn/apache/cxf/3.1.12/apache-cxf-3.1.12.zip 2.配置 CX ...

  8. final文案+美工

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476] 文案+美工: 剧情设计+题目设计+美工: 第21关: 剧情: 计算机学 ...

  9. <Spark><Running on a Cluster>

    Introduction 之前学习的时候都是通过使用spark-shell或者是在local模式运行spark 这边我们首先介绍Spark分布式应用的架构,然后讨论在分布式clusters中运行Spa ...

  10. 判断input checkbox选中

    $("#chexk").get(0).checked $("#chexk").is(':checked')