import random

num = random.random() # 生成0-1之间的随机浮点数 num2 = random.uniform(1, 10) # 生成的随机浮点数归一化到区间1-10 print(num) print(num2) 123456

(2) 生成随机字符

import random

alphabet = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*()' char = random.choice(alphabet) print(char) 12345

(3) 生成随机字符串

生成指定数量的随机字符串

import random

alphabet = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*()' characters = random.sample(alphabet, 5) print(characters) 12345

从a-zA-Z0-9生成指定数量的随机字符

import random import string

value = ''.join(random.sample(string.ascii_letters + string.digits, 8)) print(value) 12345

随机选取字符串

import random

table = ['剪刀', '石头', '布'] print(random.choice(table)) 用户8395390032432 10.0k

粉丝