random模块(随机模块)

小提示:random模块属于Python内置模块

randint( a, b )

功能

随机返回[a, b]范围内的一个整数

参数

·a:起始整数
·b:结束整数

返回值

随机返回[a, b]范围内的一个整数(范围包括a和b)

例子

import random

num = random.randint(1, 10)
print(num)

choice( seq )

功能

随机选取字符串/列表/元组中的一个元素

参数

·seq:待选取的内容(字符串/列表/元组)

返回值

字符串/列表/元组中的一个随机元素

例子

import random

num = random.choice([1,2,3,4,5])
print(num)