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. vector list map set等容器某些函数的使用区别

    map, set, vector erase的正确使用方法 一.erase 的用法区别 STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque); 另一类是以不 ...

  2. learning ddr Electrical Characteristics and AC Timing

    reference: JEDS79-3F.pdf , page:181

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

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

  4. day27 网络通信协议 tcp/udp区别

    今日主要内容: 一.网络通信协议 二.tcp udp协议下的socket 一.网络通信协议 1.1互联网的本质就是一系列的网络协议 本机IP地址('127.0.0.1',xxxx) 互联网连接的电脑互 ...

  5. sqlalchemy动态组合查询语句。

    if filter_type == 1: search = and_(GameRoom.status ==1,or_( and_(GameRoom.white_user_id == user_id, ...

  6. rnn-手写数字识别-网络结构-shape

    手写数字识别经典案例,目标是: 1. 掌握tf编写RNN的方法 2. 剖析RNN网络结构 tensorflow编程 #coding:utf-8 import tensorflow as tf from ...

  7. 牛客第三场多校 E Sort String

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...

  8. <Hadoop重装><centos><NameNode失效>

    Overview 记一次真真切切的NameNode单点故障. 学校机房着火之后,刀片机上的四台服务器,唯独就NameNode彻底宕掉了,去机房看了下硬盘都坏了.. 所以只能换一个master咯.基本上 ...

  9. pycharm运行pytest

    pycharm运行三种方式 1.以xx.py脚本方式直接执行,当写的代码里面没用到unittest和pytest框架时,并且脚本名称不是以test_开头命名的,此时pycharm会以xx.py脚本方式 ...

  10. L316 波音737Max 危机

    Boeing Scrambles To Restore Faith In Its 737 MAX Airplane After Crashes In the wake of two deadly cr ...