首页 > 代码库 > python 利用random的shuffle洗牌方法生产1个12位数的随机密码

python 利用random的shuffle洗牌方法生产1个12位数的随机密码

  1 # !/usr/bin/env python                                                           
  2 # -*- coding:utf-8 -*-                                                           
  3 import random                                                                    
  4                                                                                  
  5 schr = [chr(s) for s in range(65,91)]                                            
  6 bchr = [chr(b) for b in range(97,123)]                                           
  7 num = [str(n) for n in range(0,10)]                                              
  8 signs = [‘!‘,‘@‘,‘#‘,‘$‘,‘%‘,‘^‘,‘&‘,‘*‘,‘(‘,‘)‘,‘+‘,‘-‘,‘_‘,‘=‘,‘<‘,‘>‘,‘`‘,‘|‘,‘[‘,‘]‘,‘{‘,‘}‘,‘?‘,‘/‘]
  9 codes = schr + bchr + num + signs                                                
 10                                                                                  
 11 def shuffle(codes):                                                              
 12     for i in range(5):                                                           
 13         random.shuffle(codes)                                                    
 14     return codes                                                                 
 15                                                                                  
 16 def get_code(codes):                                                             
 17     codes = shuffle(codes)                                                       
 18     rand_start = random.randint(0,len(codes)-12)                                 
 19     rand_end = rand_start + 12                                                   
 20     return ‘‘.join(codes[rand_start:rand_end])                                   
 21                                                                                  
 22 print get_code(codes)


本文出自 “bobo的运维之路” 博客,转载请与作者联系!

python 利用random的shuffle洗牌方法生产1个12位数的随机密码