别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23

Python 标准库系列之 random 模块

This module implements pseudo-random number generators for various distributions.

random.random()

生成 0-1 的小数

>>> random.random()
0.06511225392331632
>>> random.random()
0.9063480964287944
>>> random.random()
0.1255900898753961
>>> random.random()
0.6676866041289258
1
2
3
4
5
6
7
8

random.randint(a, b)

输出 a 和 b 范围内的数,包括 a 和 b

>>> random.randint(1,2)
1
>>> random.randint(1,2)
1
>>> random.randint(1,2)
1
>>> random.randint(1,2)
2
>>> random.randint(1,2)
1
>>> random.randint(1,2)
1
1
2
3
4
5
6
7
8
9
10
11
12

random.randrange(start, stop[, step])

输出 start 到 stop-1 之间的数,可设置步长

>>> random.randrange(1,3)
2
>>> random.randrange(1,3)
1
>>> random.randrange(1,3)
2
1
2
3
4
5
6

随机验证码实例

#!/usr/bin/env python
import random
checkcode = ''
# for循环四次
for i in range(4):
    # current=0-3的数字
    current = random.randrange(0,4)
    # 如果current的值不等于i
    if current != i:
    	# 通过chr把数字转换为一个字母赋值给temp
        temp = chr(random.randint(65,90))
    else:
    	# 否则temp=0-9之间的数字
        temp = random.randint(0,9)
    # checkcode = checkcode + str(temp)
    checkcode += str(temp)
# 输出字符
print(checkcode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

执行

ansheng@ansheng-me:~$ python s.py
TCQ1
ansheng@ansheng-me:~$ python s.py
8L01
ansheng@ansheng-me:~$ python s.py
N2EB
ansheng@ansheng-me:~$ python s.py
XIDO
1
2
3
4
5
6
7
8
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 os 模块
Python 标准库系列之 shutil 模块

← Python 标准库系列之 os 模块 Python 标准库系列之 shutil 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式