python 之模块random

随机形成字母和数字组成的五位字符码。

[root@localhost python]# vim  timee.py

import random

def coder():
  code = ''
  for i in range(5):
    add = random.choice([random.randrange(10),chr(random.randrange(65,91))])  #数字和字母
    code += str(add)
  print (code)

coder()


运行结果:

[root@localhost python]# python timee.py
NYO6F
[root@localhost python]# python timee.py
MS15V
[root@localhost python]# python timee.py
16SR6
[root@localhost python]# python timee.py
9Z3SW
[root@localhost python]# python timee.py
XN0KW
[root@localhost python]# python timee.py
1PPT9
[root@localhost python]# python timee.py
E25U2

  

原文地址:https://www.cnblogs.com/lixinliang/p/8413718.html