python random

创建日期: 2023-05-05 10:41 | 作者: 风波 | 浏览次数: 14 | 分类: Python

参考:https://blog.csdn.net/lu8000/article/details/125084127

1. 整数

import random

n = random.randint(1, 100) # 1~100 的整数

2. 小数

2.1 一个随机小数 0~1

import random

n = random.random()

2.2 一个指定范围的小数

import random

n = random.uniform(5.5, 20.6) # 5.5~20.6 之间的小数

3. 从列表中随机选择 k 个数据

参考:https://www.w3schools.com/python/ref_random_choices.asp

例如随机选取3个

import random

as = ['1', '2', '3', '4', '5']

xs = random.choices(as, k=3)
14 浏览
7 爬虫
0 评论