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. ubuntu 挂载虚拟机vdi文件

    sudo apt-get  install nbd-server  nbd-client  qemu-kvm # rmmod nbd # modprobe nbd max_part=8 # qemu- ...

  2. laravel中的Auth认证:

    简介 Laravel 5.3 的 Auth 认证在 5.2 的基础上又有一些改变,本文说明如何在 Laravel 5.3 下做不同用户表的登录认证. Auth 认证原理简述 Laravel 的认证是使 ...

  3. Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...

  4. 常见的SQLALCHEMY列类型

    常见的SQLALCHEMY列类型.配置选项和关系选项   类型名称    python类型    描述 Integer int 常规整形,通常为32位 SmallInteger    int 短整形, ...

  5. 《Python》线程之锁、信号量、事件、条件、定时器、队列

    一.锁 线程为什么要有锁: += .-= 赋值操作数据不安全(要经过取值.计算.放回值,3部操作) pop .append 都是数据安全的(只有添加和删除,一次操作) 队列也是数据安全的 1.同步锁 ...

  6. bootstrapValidator常用验证规则总结

    bootstrapValidator常用验证规则总结 一 .bootstrapValidator引入 在使用bootstrapValidator前我们需要引入bootstrap和bootstrapVa ...

  7. Centos7创建用户su登录后显示为 bash-4.1$

    useradd name [root@localhost data]# su name bash-4.2$ [root@localhost ~]# cp -a /etc/skel/. /home/na ...

  8. 接口的鉴权&响应数据解密

    前言: 1.开放的接口为了避免被别人攻击,频繁刷接口,浪费服务器资源,这就涉及到签名(Signature)加密了 2.API 使用签名方法(Signature)对接口进行鉴权(Authenticati ...

  9. requests保持登录session ,cookie 和 token

    一.request提供了一个一个叫做session的类,来实现客户端和服务端的会话保持 # coding:utf-8 import requests url = "https://passp ...

  10. Building Tablet PC Applications ROB JARRETT

    Building Tablet PC Applications ROB JARRETT Tablet PC 开发,有需要PDF的留下邮箱 目录This text was added by using ...