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. js之querySelector方法

    querySelector()接受一个CSS选择符,返回匹配的第一个元素,反之则NULL. 如: var body = document.querySelector('body'); var mydi ...

  2. 熔断监控面板(Hystrix Dashboard)

    Hystrix Dashboard Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Comma ...

  3. 微信和支付宝支付模式详解及实现(.Net标准库)

    支付基本上是很多产品都必须的一个模块,大家最熟悉的应该就是微信和支付宝支付了,不过更多的可能还是停留在直接sdk的调用上,甚至和业务系统高度耦合,网上也存在各种解决方案,但大多形式各异,东拼西凑而成. ...

  4. LeetCode刷题 Flood Fill 洪水填充问题

    An  image is represented by a 2-D array of integers,each integers,each integer respresenting the sta ...

  5. Java遍历集合的几种方法分析(实现原理、算法性能、适用场合)

    概述 Java语言中,提供了一套数据集合框架,其中定义了一些诸如List.Set等抽象数据类型,每个抽象数据类型的各个具体实现,底层又采用了不同的实现方式,比如ArrayList和LinkedList ...

  6. sass进阶 @if @else if @else @for循环

    这种判断语句要配合混合宏来使用 定义下一混合宏 @mixin blockOrHidden($boolean:true) { @if $boolean { @debug "$boolean i ...

  7. dapper 简单多表查询

    public List<Book> GetBookList() { List<Book> bList = null; try { using (var t = new SqlC ...

  8. codeforce949A(顺带vector详细使用介绍)

    A. Zebras time limit per test1 second memory limit per test512 megabytes inputstandard input outputs ...

  9. adb shell按键操作(input keyevent)

    前言:input keyeven操作发送手机上常用的一些按键操作 一.keyevent事件对应数字 电话键 KEYCODE_CALL: 拨号键 KEYCODE_ENDCALL: 挂机键 KEYCODE ...

  10. MFC 添加背景图片并让控件背景透明

    /*添加背景图片*/ BOOL CTOOLDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CDialog::OnEraseBkg ...