首页 > 编程语言 > 详细

Python之random模块

时间:2018-07-22 13:27:00      阅读:175      评论:0      收藏:0      [点我收藏+]

import random

print(random.random())#(0,1)之间的浮点数

print(random.randint(1,4))#[1,4]之间的整数

print(random.randrange(1,4))#[1,4)之间的数

print(random.choice([1,‘a‘,[1,4,5,3,7],9]))#在数列里随机选一个

res=[1,‘a‘,[1,4,5,3,7],9]

print(random.sample(res,3))#在数列里随机选3个

print(random.shuffle(res))#打乱res的顺序

实例:生成随机验证码

import random


def make_code():
    while True:
        n = input(please input number>>>).strip()
        if n == q: break
        if not n.isdigit(): continue
        n = int(n)
        res = ‘‘
        for i in range(n):
            s1 = chr(random.randint(65, 90))
            s2 = str(random.randint(0, 10))
            s3 = chr(random.randint(97, 122))
            res += random.choice([s1, s2, s3])
        print(res)


make_code()

 

Python之random模块

原文:https://www.cnblogs.com/qiaoqianshitou/p/8745355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!