码迷,mamicode.com
首页 > 编程语言 > 详细

Python之random模块

时间:2018-07-22 14:11:26      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:浮点   style   sam   shuff   模块   一个   ase   ==   生成   

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模块

标签:浮点   style   sam   shuff   模块   一个   ase   ==   生成   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!