import random print(random.random()) #0.1-1 print(random.randint(1,8)) #1-8 print(random.choice("helloworld")) #h e l l o w o r l d 打散字符串,随机输出一个。 print(random.choice(["123","456","777"])) #[]这种形式???777 print(random.choices(["123","456","777"])) #[]这种形式??? str 字符串['777'] # print(help(random.shuffle)) # print(random.shuffle())######干啥的不知道? print(random.sample("helloworld",2)) #这个可以定义选出2个,并返回['e', 'l'] print(random.sample(["123","456","777"],2))#同上,,,['456', '777'] 可随机输出2个 print(random.randrange(1,200))#输出不包含200,最后一个不包含的情况用的比较多哦。
玩个随机数
import random def v_code(): code = "" for i in range(5): add_num = str(random.randrange(0,9)) add_al = chr(random.randrange(65,91))#chr转换为A-Z大写。print(chr(90))#65-90任意生成A-Z sj = random.choice([add_num,add_al,add_al.lower(),add_num])#str.lower()转换为小写,为了保证概率,将_add_num写两遍,这样,字母和数字概率一样了 code = "".join([sj,code]) return code print(v_code())