zoukankan      html  css  js  c++  java
  • python学习之-- random模块

    random模块
    random.random():随机打印一个小数
    random.randint(1,10):随机打印1-10之间的任意数字(包括1和10)
    random.randrange(1,10):随机打印1-10之间的任意数字(不包括10)
    random.sample(range(100),5):从100个数字中随机抽取5个数字以列表形式打印。可以用作随机验证码或密码使用
    如:random.sample('abcde',3) 随机生成3个字符。
    举例:生成随机验证码
    第一种写法
    1 import string,random
    2 #通过string模块生成大小写字母和0-9数字
    3 s = string.ascii_letters+string.digits
    4 #从所有字母和数字中随机提取6个数字
    5 print(''.join(random.sample(s,6)))  #验证码
    View Code

    第二种写法:
     1 import random
     2 info = ''
     3 #循环6次表示验证码为6位
     4 for i in range(6):
     5     #随机生成0-6之间的数字,不包括6,和上面的循环对应
     6     curr = random.randrange(0,6)
     7     # 如果循环中出现的数字正好和这里随机生成的数字对应上,则生成数字
     8     if curr == i:
     9         temp = random.randint(0,9)
    10     else:
    11     # 否则生成大写字符
    12         temp = chr(random.randint(65,90))
    13      #拼出6为字符为验证码
    14     info += str(temp)
    15 print(info)
    View Code
  • 相关阅读:
    有向无环图单源最短路径问题
    linux下程序编译出错解决方法
    Ceres入门笔记
    Java 中的数据结构类 Vector 和 ArrayList
    102. Binary Tree Level Order Traversal
    104. Maximum Depth of Binary Tree
    101. Symmetric Tree
    100. Same Tree
    490. The Maze
    骑士游历问题
  • 原文地址:https://www.cnblogs.com/zy6103/p/6832946.html
Copyright © 2011-2022 走看看